Ask a questionAsk a question
 

AnswerBasic Concurrent data structures question

  • Monday, October 19, 2009 6:40 PMVitaly DilmukhametovMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi2all!

    I have one conceptual question to Parallel Fx team. For example, I have 1 concurrent queue with 3 elements. And 2 threads try to dequeue 1 element simultaneously. MSDN documentation noticed:
    "Return Value Type: System . Boolean
    true if an element was removed and returned from the beggining of the ConcurrentQueue< (Of < ( T> ) > ) succesfully; otherwise, false .
    "
    Ok, and what about described situation? Both threads returns two first values from queue? Or 2nd thread returns false?

    Can you explain me, when exactly TryDequeue() method return false?

    Thank you!

Answers

  • Wednesday, October 21, 2009 2:24 PMStephen Toub - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Vitaly-

    TryDequeue tries to remove an element from the queue, and if it can, it does and returns true; if it can't, it returns false.  All of that happens atomically with respect to other operations on the queue.  So, in your example, if the queue was filled with code like:
        q.Enqueue("a");
        q.Enqueue("b");
        q.Enqueue("c");
    and then concurrently two threads try to dequeue an element, one thread will dequeue "a" and the other thread will dequeue "b"; both calls to TryDequeue will return true, because they were both able to dequeue an element.  Now, if each thread goes back to dequeue an additional element, one of the threads will dequeue "c" and return true, while the other will find the queue empty and as such TryDequeue will return false.
  • Thursday, October 22, 2009 1:38 AMStephen Toub - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Vitaly-

    ConcurrentQueue handles all of that necessary synchronization internally.  Even if both threads call TryDequeue at precisely the same moment, the behavior I described in my previous reply still holds.  No blocking is employed.  If a conflict is detected between two threadss, one of them may have to try again to retrieve the next eelement, but again, that's all handled internally as an implementation detail by the ConcurrentQueue.

All Replies

  • Wednesday, October 21, 2009 2:24 PMStephen Toub - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Vitaly-

    TryDequeue tries to remove an element from the queue, and if it can, it does and returns true; if it can't, it returns false.  All of that happens atomically with respect to other operations on the queue.  So, in your example, if the queue was filled with code like:
        q.Enqueue("a");
        q.Enqueue("b");
        q.Enqueue("c");
    and then concurrently two threads try to dequeue an element, one thread will dequeue "a" and the other thread will dequeue "b"; both calls to TryDequeue will return true, because they were both able to dequeue an element.  Now, if each thread goes back to dequeue an additional element, one of the threads will dequeue "c" and return true, while the other will find the queue empty and as such TryDequeue will return false.
  • Wednesday, October 21, 2009 3:50 PMVitaly DilmukhametovMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, Stephen!

    Thank you for reply. I think, I do not explain my idea correctly.

    What if 2 threads try to dequeue exactly in one moment of time (in one second, ms, etc) . First of it receive an element, and what about 2nd ? Will it blocked, until first is getting element, or return false because can't get an element (because 1st thread work with queue)?

    P.S. I accept, that it is not "a real scenario", and I can reproduce it, because not guarantee precision of moment of time, when threads call TryDequeue().
  • Thursday, October 22, 2009 1:38 AMStephen Toub - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Vitaly-

    ConcurrentQueue handles all of that necessary synchronization internally.  Even if both threads call TryDequeue at precisely the same moment, the behavior I described in my previous reply still holds.  No blocking is employed.  If a conflict is detected between two threadss, one of them may have to try again to retrieve the next eelement, but again, that's all handled internally as an implementation detail by the ConcurrentQueue.
  • Thursday, October 22, 2009 2:34 AMVitaly DilmukhametovMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, Stephen,

    Ok, so 2nd thread is try again to retrieve an element and dequeued it. Thanks!