Synchronizing a Queue

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

    #1

    Synchronizing a Queue

    I have not been able to find a good answer about the
    System.Collecti ons.Queue.Synch ronized() method and how it actually works or
    is to be used.

    If I create my Queue in the following manner:

    System.Collecti ons.Queue myQueue = System.Collecti ons.Queue.Synch ronized(new
    System.Collecti ons.Queue());

    is it a Thread-Safe queue no matter where it is used? Or do I have to call
    Synchronized() and use the Queue that it returns everytime I want to use the
    Queue.

    Thanks,
    Chuck


  • Joshua Flanagan

    #2
    Re: Synchronizing a Queue

    The operations within myQueue will always be thread safe.

    Be very careful what assumptions you make about using a synchronized
    Queue. A synchronized collection simply means that each individual
    method (Enqueue, Dequeue) is safe to call from multiple threads without,
    damaging the internal state. But multiple calls to the methods are not
    thread safe without additional coding.

    For example, if you write the code:

    if (myQueue.Count > 0){
    item = myQueue.Dequeue ()
    }

    it is still possible that you will get an InvalidOperatio nException when
    multiple threads have access to myQueue. Another thread could remove
    the last item in between the check on .Count and the actual Dequeue().
    If you want the above code to work in a thread safe manner, you would
    still need to use a locking mechanism.

    For example:
    lock(myQueue.Sy ncRoot){
    if (myQueue.Count > 0){
    item = myQueue.Dequeue ()
    }
    }




    Chuck wrote:[color=blue]
    > I have not been able to find a good answer about the
    > System.Collecti ons.Queue.Synch ronized() method and how it actually works or
    > is to be used.
    >
    > If I create my Queue in the following manner:
    >
    > System.Collecti ons.Queue myQueue = System.Collecti ons.Queue.Synch ronized(new
    > System.Collecti ons.Queue());
    >
    > is it a Thread-Safe queue no matter where it is used? Or do I have to call
    > Synchronized() and use the Queue that it returns everytime I want to use the
    > Queue.
    >
    > Thanks,
    > Chuck
    >
    >[/color]

    Comment

    • Chuck

      #3
      Re: Synchronizing a Queue

      Thanks for the information... it was most helpful!

      chuck

      "Joshua Flanagan" <josh@msnews.co m> wrote in message
      news:uu2Js42VFH A.2616@TK2MSFTN GP14.phx.gbl...[color=blue]
      > The operations within myQueue will always be thread safe.
      >
      > Be very careful what assumptions you make about using a synchronized
      > Queue. A synchronized collection simply means that each individual method
      > (Enqueue, Dequeue) is safe to call from multiple threads without, damaging
      > the internal state. But multiple calls to the methods are not thread safe
      > without additional coding.
      >
      > For example, if you write the code:
      >
      > if (myQueue.Count > 0){
      > item = myQueue.Dequeue ()
      > }
      >
      > it is still possible that you will get an InvalidOperatio nException when
      > multiple threads have access to myQueue. Another thread could remove the
      > last item in between the check on .Count and the actual Dequeue(). If you
      > want the above code to work in a thread safe manner, you would still need
      > to use a locking mechanism.
      >
      > For example:
      > lock(myQueue.Sy ncRoot){
      > if (myQueue.Count > 0){
      > item = myQueue.Dequeue ()
      > }
      > }
      >
      >
      >
      >
      > Chuck wrote:[color=green]
      >> I have not been able to find a good answer about the
      >> System.Collecti ons.Queue.Synch ronized() method and how it actually works
      >> or is to be used.
      >>
      >> If I create my Queue in the following manner:
      >>
      >> System.Collecti ons.Queue myQueue =
      >> System.Collecti ons.Queue.Synch ronized(new System.Collecti ons.Queue());
      >>
      >> is it a Thread-Safe queue no matter where it is used? Or do I have to
      >> call Synchronized() and use the Queue that it returns everytime I want to
      >> use the Queue.
      >>
      >> Thanks,
      >> Chuck[/color][/color]


      Comment

      Working...