Sample Code: Blocking Queue

  • Thread starter Thread starter Zane Thomas [.NET/C# MVP]
  • Start date Start date
Z

Zane Thomas [.NET/C# MVP]

BlockingQueue implements a single-writer single-reader queue, something I find
myself needing frequently when working with multiple threads and internet
protocols.

After an instance of BlockingQueue is created the reader can invoke Dequeue and
will be blocked until either there is some object in the queue, or until
BlockingQueue.EOF is true. Setting EOF will eventually result in a null being
returned from Dequeue, but only after all items Enqueued have been retrieved

Source in the Free Things & Other Stuff folder at www.abderaware.com


--
What the caterpillar calls the end,
the rest of the world calls a butterfly.
~ Lao-tzu

Components For Thinkers
www.abderaware.com
zane @at@ abderaware .dot. com
 
Zane Thomas said:
BlockingQueue implements a single-writer single-reader queue, something I find
myself needing frequently when working with multiple threads and internet
protocols.

After an instance of BlockingQueue is created the reader can invoke Dequeue and
will be blocked until either there is some object in the queue, or until
BlockingQueue.EOF is true. Setting EOF will eventually result in a null being
returned from Dequeue, but only after all items Enqueued have been retrieved

At the moment, it's not quite threadsafe - you don't synchronize at all
before setting m_Queue to null or checking whether or not it *is* null.
In addition, the queue could be closed *after* the check for
m_Queue==null but *before* calling m_Queue.Dequeue(), in which case you
get a NullReferenceException.
 
Jon,
At the moment, it's not quite threadsafe ...

That's true, and while not an excuse for code put out for general use, it's not
an issue the way I use it. I'll make it thread-safe tonight or thereabouts.
Thanks!


--
What the caterpillar calls the end,
the rest of the world calls a butterfly.
~ Lao-tzu

Components For Thinkers
www.abderaware.com
zane @at@ abderaware .dot. com
 
Back
Top