コード例 #1
0
        public void TestConcurrentDictionaryultiThreadInserts()
        {
            int counter = nThreads;

            ThreadPool.SetMaxThreads(nThreads, nThreads);
            long mem = GC.GetTotalMemory(true);
            var  bt  = new ConcurrentDictionary <string, string>();

            for (int i = 0; i < nThreads; i++)
            {
                int threadNo = i;
                ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                {
                    for (int j = 0; j < totalItems; j++)
                    {
                        if (j % nThreads == threadNo)
                        {
                            bt.TryAdd(j.ToString(), j.ToString());
                        }
                    }
                    Interlocked.Decrement(ref counter);
                }));
            }

            UnitTestMultithreadedConcurrentTrieIterator.WaitThreadPoolCompletion(ref counter);
            var diff = GC.GetTotalMemory(true) - mem;

            for (int j = 0; j < totalItems; j++)
            {
                string lookup;
                TestHelper.assertTrue(bt.TryGetValue(j.ToString(), out lookup));
                TestHelper.assertEquals(j, int.Parse(lookup));
            }
        }
コード例 #2
0
        public void TestHashCollisionsRemove()
        {
            ConcurrentTrieDictionary <Object, Object> bt = new ConcurrentTrieDictionary <Object, Object>();
            int count = 50000;

            for (int j = 0; j < count; j++)
            {
                Object[] objects = UnitTestMultithreadedConcurrentTrieIterator.getObjects(j);
                foreach (Object o in objects)
                {
                    bt.put(o, o);
                }
            }

            for (int j = 0; j < count; j++)
            {
                Object[] objects = UnitTestMultithreadedConcurrentTrieIterator.getObjects(j);
                foreach (Object o in objects)
                {
                    bt.remove(o);
                }
            }

            TestHelper.assertEquals(0, bt.size());
            //TestHelper.assertTrue(bt.isEmpty());
        }
コード例 #3
0
        public void TestConcurrentHashTrieMultiThreadAddDelete()
        {
            for (int j = 0; j < RETRIES; j++)
            {
                var bt = new ConcurrentTrieDictionary <Object, Object>();
                {
                    ThreadPool.SetMaxThreads(N_THREADS, N_THREADS);
                    int counter = N_THREADS;
                    for (int i = 0; i < N_THREADS; i++)
                    {
                        int threadNo = i;
                        ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                        {
                            for (int jj = 0; jj < COUNT; jj++)
                            {
                                if (jj % N_THREADS == threadNo)
                                {
                                    bt.put(jj, jj);
                                }
                            }
                            Interlocked.Decrement(ref counter);
                        }));
                    }
                    UnitTestMultithreadedConcurrentTrieIterator.WaitThreadPoolCompletion(ref counter);

                    TestHelper.assertEquals(COUNT, bt.size());
                    counter = N_THREADS;

                    for (int i = 0; i < N_THREADS; i++)
                    {
                        int threadNo = i;
                        ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                        {
                            for (int jj = 0; jj < COUNT; jj++)
                            {
                                if (jj % N_THREADS == threadNo)
                                {
                                    bt.remove(jj);
                                }
                            }
                            Interlocked.Decrement(ref counter);
                        }));
                    }
                    UnitTestMultithreadedConcurrentTrieIterator.WaitThreadPoolCompletion(ref counter);

                    counter = N_THREADS;
                    TestHelper.assertEquals(0, bt.size());
                    for (int i = 0; i < N_THREADS; i++)
                    {
                        int threadNo = i;
                        ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                        {
                            try
                            {
                                for (int jj = 0; jj < COUNT; jj++)
                                {
                                    if (jj % N_THREADS == threadNo)
                                    {
                                        try
                                        {
                                            bt.put(jj, jj);
                                            if (!bt.containsKey(jj))
                                            {
                                                Assert.Fail("Key j not found");
                                            }
                                            bt.remove(jj);
                                            if (bt.containsKey(jj))
                                            {
                                                Assert.Fail("Key jj found and should have been removed");
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            Assert.Fail(e.Message);
                                        }
                                    }
                                }
                            }
                            finally
                            {
                                Interlocked.Decrement(ref counter);
                            }
                        }));

                        TestHelper.assertEquals(0, bt.size());
                    }
                    UnitTestMultithreadedConcurrentTrieIterator.WaitThreadPoolCompletion(ref counter);
                }
            }
        }