Lee_666


Re: Visual C# General Threading and Queue access issues.

OmegaMan

The short answer is no, the queue is not thread safe. Place a lock around the enque and deque methods and that should be all that is needed. See how that is done in MSDN Threading Tutorial which details the lock and other ways of ensuring thread safety.





Re: Visual C# General Threading and Queue access issues.

Peter Ritchie

Nothing can be thread-safe for all threading situations. The general tendancy of .NET 2.0 and greater is to not even try performing any synchronization; it's up to the application logic to decide how it should synchronize it's complex invariants.

If you've generally got one set of threads reading the queue and one set of threads writing to the queue (or one set reading and writing and another set just writing or just reading) you might want to consider using something like ReaderWriterLock. In .NET 2.0 it's not the fastest option; so if performance is a huge issue and you're not moving to .NET 3.x any time soon, you might want to read Jeffrey Richter's Concurrent Affairs: Reader/Writer Locks and ResourceLock Library where he describes his alternative.






Re: Visual C# General Threading and Queue access issues.

Lee_666

Thanks for the anwsers folks.

A bit of extra info - I have one timer event inside a singleton class which reads this Queue, and empties it once a minute. I have any number of instances of a different class adding data to this queue every 3 minutes each, again via a timer. Performance is not an issue.

Is there anything wrong with just wrapping the enqueue / dequeue calls with a lock Is there a specific reason for using a ReaderWriterLock object

Lee.





Re: Visual C# General Threading and Queue access issues.

Peter Ritchie

Depends on what you consider "wrong", I suppose. In the scenario you describe it sounds like it won't cause you an undue problems. Generally, with a reader/writer model you don't want to lock out readers while reading, only while writing. But, it sounds like you've got many writers, so a simple lock(lockObject) should suffice.




Re: Visual C# General Threading and Queue access issues.

Lee_666

Lovely, thanks for that.

It will be middle of next week before I can properly test with multiple writers due to equipment issues, but I really wanted to get the Queue processing code as complete as possible. This info should allow me to do that.

Thanks again for the assistance, I'll go read up on the ReaderWriterLock class anyway, as it's bound to come in useful!

Lee.





Re: Visual C# General Threading and Queue access issues.

Mark Dawson

Hi,

I also have an example of a producer/consumer pattern, that allows a thread to put items into a queue while other multiple worker thread take items out of the queue and process the items: http://themightycoder.spaces.live.com/blog/cns!EBFBA22CD769E10B!158.entry might be of interest.

Mark.






Re: Visual C# General Threading and Queue access issues.

Lee_666

Cool, I'll check it out! My primary aim is to get this code functional asap, but beyond that, the more bulletproof I can make it the better - it'll be running as a service on a customers machine 24/7, so I do need to think about stability.