China Naming Network - Ziwei Dou Shu - Is queue entry and exit thread-safe?
Is queue entry and exit thread-safe?
Queues themselves are not thread-safe. There are two ways to ensure thread safety:
1. Manual locking.
Queue my collection = new Queue();
Lock (myCollection. Synchronization root)
{
Foreach (object item in my collection)
{
//Insert your code here.
}
}
2. Call the Synchronized method, and you will get a thread-safe queue.
Queue mySyncdQ = Queue。 Synchronization (myq);