/// <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 FindMaxTest() { Assert.AreEqual(0, queue.Count); Assert.Throws <NoSuchItemException>(() => queue.FindMax()); Random rng = new Random(); if (prefill) { for (int i = 0; i < n; i++) { queue.Add(rng.Next(1000)); } } Thread[] threads = new Thread[threadCount]; for (int i = 0; i < threadCount; i++) { Thread t = new Thread(() => { for (int y = 0; y < n; y++) { queue.FindMax(); } }); 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((prefill ? n : 0), queue.Count); }
public void EmptyQueueFindMaxTest() { Assert.AreEqual(0, queue.Count); Assert.Throws <NoSuchItemException>(() => queue.FindMax()); }