Is Queue.SyncRoot required for a simple Enqueue / Dequeue in threads?

I have WorkerThreadone that does the job on the items in the queue and a few MiningThreadthat create the stuff that needs to be done with WorkerThread.

To summarize: one stream of Dequeues and several Enqueue.

Do I need to use the synchronization pattern suggested in msdn, or am I sure of the flow in this particular scenario?

From msdn a simple template with synchronization

Queue myCollection = new Queue();
lock(myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
+3
source share
2 answers

Yes, you need to sync.

There is a .NET4 queue in which you do not need synchronization called "ConcurrentQueue": http://msdn.microsoft.com/en-us/library/dd267265.aspx

.NET4, .

Update: , .NET4, BlockingCollection, Producer/Consumer: http://msdn.microsoft.com/en-us/library/dd267312.aspx

.NET4 tough...:)

+6

Queue , . SyncRoot Synchronized .

+1

All Articles