Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Question on FIFO Order of concurrent_queue

ksgokul
Beginner
466 Views
Hi,
I have a requirement where in a session can move around different threads and enqueue in a particular queue. Can i entrust concurrent_queue for the FIFO order? From my initial reading i find that the concurrent_queue provides the FIFO order only for a particular producer thread.
I want to have FIFO order applicable for the session instead of the thread. Is there a easy way to accomplish it?
Thanks,
Gokul.
0 Kudos
1 Solution
Alexey-Kukanov
Employee
466 Views
If your session is only owned by one thread at a time, and it *never* happens that multiple threads enqueue the data into concurrent_queue in undeterministic order, then it is the same for the queue as if there was a single producer, and so FIFO is guaranteed.
It's the same what Dmitry wrote: if thread T1 pushes Ai, then transfers ownership to thread T2, and then T2 pushes Bj, a consumer will take Ai first, then Bj, for any i and j. If you have multiple consumer threads, the above holds true for any of these threads (i.e. no thread can dequeue Bj and then Ai).
However there is no global ordering guarantees,e.g. A_last and B_first can be dequeued concurrently by different threads, and whichever finishes first is unknown.

View solution in original post

0 Kudos
5 Replies
Dmitry_Vyukov
Valued Contributor I
466 Views
If you have a single consumer thread, and several producer threads, and message A is enqueued from thread 1 before message B from thread 2, then the consumer must receive A before B.
0 Kudos
Alexey-Kukanov
Employee
467 Views
If your session is only owned by one thread at a time, and it *never* happens that multiple threads enqueue the data into concurrent_queue in undeterministic order, then it is the same for the queue as if there was a single producer, and so FIFO is guaranteed.
It's the same what Dmitry wrote: if thread T1 pushes Ai, then transfers ownership to thread T2, and then T2 pushes Bj, a consumer will take Ai first, then Bj, for any i and j. If you have multiple consumer threads, the above holds true for any of these threads (i.e. no thread can dequeue Bj and then Ai).
However there is no global ordering guarantees,e.g. A_last and B_first can be dequeued concurrently by different threads, and whichever finishes first is unknown.
0 Kudos
jimdempseyatthecove
Honored Contributor III
466 Views

Also, with respect to concurrent push:

There is a point within the enqueue code (push code), that could be called (considered as) an "atomic line of demarcation". The implementation of which can be determined by looking at the code, but could be as simple as an atomic increment of a variable, or as heavy as a critical section. The order in which the threads pass through this atomic line of demarcation, and which asserts the FIFO order, is not necessarily the order in which the threads began the push statement. IOW there is some code to pass through to get up to the "atomic line of demarcation".

Additionally, and this again is dependent on implementation, it may be possible for the 2nd thread in concurrent push situation, to exit the FIFO push prior to the 1st thread exiting the FIFO push.

When you need to control the moment in time for this ordering, then youmay have to roll your own code or modify the TBB code to suit your requirements.

Jim Dempsey

0 Kudos
ksgokul
Beginner
466 Views
Thanks Dmitriy for the response. Yes we have only one consumer thread.
Thanks,
Gokul.
0 Kudos
ksgokul
Beginner
466 Views
Thanks.. That was very clear....
0 Kudos
Reply