/// <summary> /// The work done by each thread /// - It writes the cubed value of each element in the queue to a disposable file in the folder specified as OUTPUT_FOLDER. /// The writing to the file is done in order to take time and ensure that the code is not optimized away by the compiler. /// In the real benchmarking version this would not be necessary. /// </summary> /// <param name="state"></param> private void Work(object state) { Queue <int> threadQueue = (Queue <int>)state; // Cast the state to a Queue. barrier.SignalAndWait(); // Ensure that tasks are submitted and ready to begin work at the same time Random random = new Random(); // Do some work, write to file to avoid the compiler recognizing the dead code. while (threadQueue.Count > 0) { int option = random.Next(0, 100); var element = threadQueue.Dequeue(); if (option >= (100 - config.CurrentPercentageInsert)) { dictionary.Add(element); } else if (option >= (100 - config.CurrentPercentageInsert - config.CurrentPercentageDeleteMin)) { try { dictionary.DeleteMin(); } catch (NoSuchItemException) { } } else if (option >= (100 - config.CurrentPercentageInsert - config.CurrentPercentageDeleteMin - config.CurrentPercentageDeleteMax)) { try { dictionary.DeleteMax(); } catch (NoSuchItemException e) { } } else if (option >= (100 - config.CurrentPercentageInsert - config.CurrentPercentageDeleteMin - config.CurrentPercentageDeleteMax - config.CurrentPercentageGetMin)) { try { dictionary.FindMin(); } catch (Exception) { } } else { try { dictionary.FindMax(); } catch (Exception) { } } } barrier.SignalAndWait();// Let main thread know we are done }
public void DeleteMinTest() { Assert.Throws <NoSuchItemException>(() => queue.DeleteMin()); if (prefill) { Random rng = new Random(); for (int i = 0; i < n * threadCount; i++) { queue.Add(rng.Next(10000)); } } Thread[] threads = new Thread[threadCount]; for (int i = 0; i < threadCount; i++) { Thread t = new Thread(() => { Random random = new Random(); for (int y = 0; y < n; y++) { queue.DeleteMin(); } }); threads[i] = t; } for (int i = 0; i < threads.Length; i++) { threads[i].Start(); } for (int i = 0; i < threads.Length; i++) { threads[i].Join(); } Assert.IsTrue(queue.Check()); Assert.AreEqual(0, queue.Count); }
public void EmptyQueueDeleteMinTest() { Assert.AreEqual(0, queue.Count); Assert.Throws <NoSuchItemException>(() => queue.DeleteMin()); }