c# - does Monitor.Wait Needs synchronization? -


I have developed a generic manufacturer-consumer queue, which has developed pulses by the monitor in the following manner:

ENQ:

Public Zero Enqui Task (T Task) {_workerQueue.Enqueue (Task); Monitor.Pulse (_locker); }

The dequeue:

  Private T Dequeue () {T dequeueItem; If (_workerQueue.Count> gt; 0) {_workerQueue.TryDequeue (dequeueItem outside); If (dequeueItem! = Tap) dequeueItem back; } While (_workerQueue.Count == 0) {Monitor.Wait (_locker); } _workerQueue.TryDequeue (dequeueItem outside); Return dequeueItem; }  

The waiting section produces the following synchronization lock exception: "Object Synchronization Method was called from an Unsinkled Block of Code" Do I Need to Integrate It? Why ? Is it better to use Slim version of Manual ResetAventS or .NET 4.0?

Yes, the current thread needs to be monitored either to wait Or Pulse , to call in the form of documents. (That's why you also need to be locked for the pulse.) Why I do not know this, I do not know about it, but it is similar in Java. I have found that I I want to do this, however, to clean up the calling code.

Note that wait releases the monitor automatically, then pulse , then returns to the monitor before returning.

Instead of using ManualResetEvent or AutoResetEvent - you can, but personally I like monitor By using methods, I do not need some other features of waiters (such as wait for atoms for any / all multiple handles).


Comments