Note that there are some explanatory texts on larger screens.

plurals
  1. POMonitor.Wait() causing elements of the queue to be skipped
    text
    copied!<p>I'm trying to pass elements from one queue to the next as space becomes available. The FixedSizeQueue is from an example I saw <a href="https://stackoverflow.com/questions/530211/creating-a-blocking-queuet-in-net">here</a>. I was printing out the elements as they are added and removed from the queue but it turns out that some of them are skipped. I think it happens when Monitor.Wait(queue) is called. Can anyone help me to solve this?</p> <pre><code>class FixedSizeQueue&lt;T&gt; : Queue&lt;T&gt; { private readonly Queue&lt;T&gt; queue = new Queue&lt;T&gt;(); private readonly int maxSize; public FixedSizeQueue(int maxSize) { this.maxSize = maxSize; } public new void Enqueue (T item) { lock (queue) { while (queue.Count &gt;= maxSize) { Monitor.Wait(queue); } queue.Enqueue(item); if (queue.Count == 1) { // wake up any blocked dequeue Monitor.PulseAll(queue); } } } public new T Dequeue() { lock (queue) { while (queue.Count == 0) { Monitor.Wait(queue); } T item = queue.Dequeue(); if (queue.Count == maxSize - 1) { Monitor.PulseAll(queue); } return item; } } } class Program { static void AddCustomerToWaitingList(Queue&lt;Customer&gt; customers, FixedSizeQueue&lt;Customer&gt; waitingList) { Customer customer = new Customer(); for (int i = 0; i &lt; 40; i++) { customer = customers.Dequeue(); waitingList.Enqueue(customer); } } static void AddCustomerToEatingList(FixedSizeQueue&lt;Customer&gt; waitingList, FixedSizeQueue&lt;Customer&gt; eatingList) { Customer customer = new Customer(); for (int i = 0; i &lt; 40; i++) { customer = waitingList.Dequeue(); eatingList.Enqueue(customer); } } static void RemoveCustomerFromWaitingList(FixedSizeQueue&lt;Customer&gt; waitingList) { Customer customer = new Customer(); for (int i = 0; i &lt; 40; i++) { customer = waitingList.Dequeue(); } } static void RemoveCustomerFromEatingList(FixedSizeQueue&lt;Customer&gt; eatingList) { Customer customer = new Customer(); foreach(Customer c in eatingList) { customer = eatingList.Dequeue(); } } static void Main(string[] args) { Chef chef = new Chef(); Customer customer = new Customer(); Queue&lt;Customer&gt; customers = new Queue&lt;Customer&gt;(40); FixedSizeQueue&lt;Customer&gt; eatingList = new FixedSizeQueue&lt;Customer&gt;(7); FixedSizeQueue&lt;Customer&gt; waitingList = new FixedSizeQueue&lt;Customer&gt;(12); Thread cafeClock = new Thread(new ThreadStart(Cafe.StartCounting)); Thread addCustomerToWaitingList = new Thread(() =&gt; AddCustomerToWaitingList(customers, waitingList)); Thread removeCustomerFromWaitingList = new Thread(() =&gt; RemoveCustomerFromWaitingList(waitingList)); Thread addCustomerToEatingList = new Thread(() =&gt; AddCustomerToEatingList(waitingList,eatingList)); Thread removeCustomerFromEatingList = new Thread(() =&gt; RemoveCustomerFromEatingList(eatingList)); for (int i = 1; i &lt;= 40; i++) { customer.Id = i; customers.Enqueue(customer); customer = new Customer(); } addCustomerToWaitingList.Start(); removeCustomerFromWaitingList.Start(); addCustomerToEatingList.Start(); removeCustomerFromEatingList.Start(); Console.WriteLine("End"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload