How to use Queue.Synchronized
-
It's not clear to me how I am expected to use Queue Synchronization. Is this a general state, which makes the Queue thread safe? Or is this a short, temporary measure I use to lock the queue before I read or write it, and immediately release it when I'm done? The documentation and samples are too short and don't go into detail on this. So my question is: do I simply wrap my Queue with a synchronized wrapper and leave it on, doing all further access through that wrapper? Or do I only wrap the Queue when I'm accessing it, and immediately drop the wrapper when I'm done? Do I pass the wrapper to other objects, or only the original Queue? Thanks for any help here.
-
It's not clear to me how I am expected to use Queue Synchronization. Is this a general state, which makes the Queue thread safe? Or is this a short, temporary measure I use to lock the queue before I read or write it, and immediately release it when I'm done? The documentation and samples are too short and don't go into detail on this. So my question is: do I simply wrap my Queue with a synchronized wrapper and leave it on, doing all further access through that wrapper? Or do I only wrap the Queue when I'm accessing it, and immediately drop the wrapper when I'm done? Do I pass the wrapper to other objects, or only the original Queue? Thanks for any help here.
Once you call Synchronized(), you should use the returned wrapper everywhere you would have used the original unsynchronized queue. Use of the queue through the wrapper will be thread safe for all "single operation" methods on the queue. Make sure Synchronized() is really what you need for a thread safe queue. Enumerating and indexing the queue aren't going to be thread safe so you may need additional locks. Making your own thread safe wrapper (or derived) queue class can be better in some situations. It allows you to implement locks the way you need to (like using a ReaderWriterLockSlim for producer/consumer thread access) and provide properly locked enumerating methods. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Once you call Synchronized(), you should use the returned wrapper everywhere you would have used the original unsynchronized queue. Use of the queue through the wrapper will be thread safe for all "single operation" methods on the queue. Make sure Synchronized() is really what you need for a thread safe queue. Enumerating and indexing the queue aren't going to be thread safe so you may need additional locks. Making your own thread safe wrapper (or derived) queue class can be better in some situations. It allows you to implement locks the way you need to (like using a ReaderWriterLockSlim for producer/consumer thread access) and provide properly locked enumerating methods. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks for the response. Okay, so the Sync wrapper persistently replaces the Q itself, and it is the object I should pass around between methods, etc. That was not entirely clear to me before. This should probably work for what I'm doing; I spawn a listener object on a new thread, which listens for events from a COM object. My main thread passes various Queues to the spawned object (actually, sets them as properties), and the spawned object, when it receives messages, puts a notation in the queue(s). My main thread polls the queue, dequeuing any notations it finds. So there's no enumerating or indexing, I think. Of course, I haven't written it yet... :-)
-
Thanks for the response. Okay, so the Sync wrapper persistently replaces the Q itself, and it is the object I should pass around between methods, etc. That was not entirely clear to me before. This should probably work for what I'm doing; I spawn a listener object on a new thread, which listens for events from a COM object. My main thread passes various Queues to the spawned object (actually, sets them as properties), and the spawned object, when it receives messages, puts a notation in the queue(s). My main thread polls the queue, dequeuing any notations it finds. So there's no enumerating or indexing, I think. Of course, I haven't written it yet... :-)
Multiple writer threads and a single reader thread, right? The Synchronized() queue should indeed be fine for that. Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: