Sample Code: Blocking Queue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zane Thomas [.NET/C# MVP]

    #1

    Sample Code: Blocking Queue


    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.E OF 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

    zane @at@ abderaware .dot. com
  • Jon Skeet

    #2
    Re: Sample Code: Blocking Queue

    Zane Thomas [.NET/C# MVP] <zane@abderawar e.com> wrote:[color=blue]
    > 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.E OF is true. Setting EOF will eventually result in a null being
    > returned from Dequeue, but only after all items Enqueued have been retrieved[/color]

    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 NullReferenceEx ception.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Zane Thomas [.NET/C# MVP]

      #3
      Re: Sample Code: Blocking Queue

      Jon,
      [color=blue]
      >At the moment, it's not quite threadsafe ...[/color]

      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

      zane @at@ abderaware .dot. com

      Comment

      Working...