Implementing thread safe Queue collection object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Macca

    #1

    Implementing thread safe Queue collection object

    Hi,

    I've been reading the online documentation for the System.Collecti ons.Queue
    object on how to implement a threadsafe Queue object.

    I understand the basics, by using the wrapper returned by Synchronised
    method, but I'm not sure how to implement it between 2 threads. Has anyone
    got any suggestions or examples?

    Thanks
    Macca
  • Peter Bromberg [C# MVP]

    #2
    RE: Implementing thread safe Queue collection object

    When you say "betweeen 2 threads" I'm not sure I understand the intent. The
    samples online are very simple:

    Queue myCollection = new Queue();
    lock( myCollection.Sy ncRoot ) {
    foreach ( Object item in myCollection ) {
    // Insert your code here.
    }
    }

    Until the closing brace of the lock statement, no other thread can operate
    on the Queue.
    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:

    UnBlog:





    "Macca" wrote:
    [color=blue]
    > Hi,
    >
    > I've been reading the online documentation for the System.Collecti ons.Queue
    > object on how to implement a threadsafe Queue object.
    >
    > I understand the basics, by using the wrapper returned by Synchronised
    > method, but I'm not sure how to implement it between 2 threads. Has anyone
    > got any suggestions or examples?
    >
    > Thanks
    > Macca[/color]

    Comment

    • Nick Hounsome

      #3
      Re: Implementing thread safe Queue collection object


      "Macca" <Macca@discussi ons.microsoft.c om> wrote in message
      news:C72A2CCB-E79B-4D68-84B4-9D8EEDBB5AFC@mi crosoft.com...[color=blue]
      > Hi,
      >
      > I've been reading the online documentation for the
      > System.Collecti ons.Queue
      > object on how to implement a threadsafe Queue object.
      >
      > I understand the basics, by using the wrapper returned by Synchronised
      > method, but I'm not sure how to implement it between 2 threads. Has anyone
      > got any suggestions or examples?
      >
      > Thanks
      > Macca[/color]

      If you mean to send stuff from one thread to another then you need to write
      your own wrapper and use
      Monitor.Wait(q) to wait for the Q to become non-empty and
      Monitor.PulseAl l(q) when you put something on it.

      i.e. something like:

      class MyQ
      {
      private Queue q;

      public void Enqueue(MyClass x)
      {
      lock(q)
      {
      q.Enqueue(x);
      Monitor.PulseAl l(q);
      }
      }

      public MyClass Dequeue(MyClass x)
      {
      lock(q)
      {
      while(q.Empty)
      Monitor.Wait(q) ;
      return (MyClass)q.Dequ eue();
      }
      }
      }

      Note that Synchronized is not used as the Queue is not exposed and locking
      is handled in the methods.


      Comment

      • Macca

        #4
        RE: Implementing thread safe Queue collection object

        Thanks for the reply Peter.

        What I mean is that I have one thread that adds entries to the queue while
        another takes them off and processes them. I'm assuming the queue will be a
        shared item between the 2 threads although not sure exactly where is best
        place to store it. In first thread?

        While items are being processed it should be possible to add entries to the
        queue at the same time although if processing thread is fast enough this may
        not be a requirement.

        I assume the processing thread will have to poll the queue to see if there
        is anything in the queue first although it would be nice to have this event
        driven rather than having to poll.

        I would have thought this is not an uncommon scenario, one thread building
        up the queue while another extracts entries and processes them?

        What I dont want is the processing of the entries to block entries being put
        on the queue therefore the need for two threads.

        What do you think?

        Regards
        Macca

        "Peter Bromberg [C# MVP]" wrote:
        [color=blue]
        > When you say "betweeen 2 threads" I'm not sure I understand the intent. The
        > samples online are very simple:
        >
        > Queue myCollection = new Queue();
        > lock( myCollection.Sy ncRoot ) {
        > foreach ( Object item in myCollection ) {
        > // Insert your code here.
        > }
        > }
        >
        > Until the closing brace of the lock statement, no other thread can operate
        > on the Queue.
        > Peter
        >
        > --
        > Co-founder, Eggheadcafe.com developer portal:
        > http://www.eggheadcafe.com
        > UnBlog:
        > http://petesbloggerama.blogspot.com
        >
        >
        >
        >
        > "Macca" wrote:
        >[color=green]
        > > Hi,
        > >
        > > I've been reading the online documentation for the System.Collecti ons.Queue
        > > object on how to implement a threadsafe Queue object.
        > >
        > > I understand the basics, by using the wrapper returned by Synchronised
        > > method, but I'm not sure how to implement it between 2 threads. Has anyone
        > > got any suggestions or examples?
        > >
        > > Thanks
        > > Macca[/color][/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Implementing thread safe Queue collection object

          Macca wrote:[color=blue]
          > Thanks for the reply Peter.
          >
          > What I mean is that I have one thread that adds entries to the queue while
          > another takes them off and processes them. I'm assuming the queue will be a
          > shared item between the 2 threads although not sure exactly where is best
          > place to store it. In first thread?[/color]

          You don't "store" items in threads.

          It sounds like you really want a producer/consumer queue. See half way
          down
          http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml for some
          sample code.
          [color=blue]
          > While items are being processed it should be possible to add entries to the
          > queue at the same time although if processing thread is fast enough this may
          > not be a requirement.
          >
          > I assume the processing thread will have to poll the queue to see if there
          > is anything in the queue first although it would be nice to have this event
          > driven rather than having to poll.[/color]

          The version I've got on the page referenced above blocks on the call to
          Consume until a work item is ready. You'd therefore end up with a
          thread which did nothing but consume work items. You should be able to
          modify the code appropriately if you want though.

          Jon

          Comment

          Working...