Posts

Showing posts with the label Queue

Thread Synchronized Queing

Today's topic gets back to some multi-threading. I find it very useful to have threads pull work from a queue and have one master assignment manager feed the queue. However the standard System.Collections.Generic.Queue is a little inadequate for me because it is not fully thread-safe. I found it a little disheartening that the Queue class members are not overridable either. So we write our own, here are my requirements... The queue must synchronize multiple writer threads writing elements to the queue. The queue must synchronize multiple reader threads reading from the queue. If there is nothing in the queue, a reader thread must wait for something or timeout. Those waiting on the queue can be “interrupted”. Of course, no two readers will ever get the same element off the queue. So let's start coding... I start with a new class called SyncQueue in my own name space and I will make it generic. namespace QueueExample { public class SyncQueue { public void Enqueue(T element)...