boost::mutex synchronization problem
I've been trying to figure out if there's a simple way using condition variables and mutexes to design a function with the following properties (cliffs notes first, and then longer version in case it's not enough info): 1) there is a section of code in the function such that two threads cannot enter that section at the same time. 2) after leaving the section of code, the thread should wait for something to happen (signalled by another thread) 3) the waits in step 2 should be awoken in the order they occured. Basically there are many threads that may want to post requests to read/write from the network. So I have a bounded buffer implementation that holds "work items", which can be of read requests or write requests. writes return immediately, but reads block until the read is finished, which is determined by when the actual network thread processes the work item and signals something. read requests should be able to enter the queue regardless of whether another read request is already pending, but obviously the blocked threads should awake in the same order that items were inserted into the queue. I thought of making a condition variable for each read work item, and storing it in the work item that goes in the queue, so the network thread can notify that. But I feel like there should be a better way that doesn't require potentially arbitrary numbers of condition variables. Thanks
Zachary Turner
I've been trying to figure out if there's a simple way using condition variables and mutexes to design a function with the following properties (cliffs notes first, and then longer version in case it's not enough info):
1) there is a section of code in the function such that two threads cannot enter that section at the same time. 2) after leaving the section of code, the thread should wait for something to happen (signalled by another thread) 3) the waits in step 2 should be awoken in the order they occured.
There's nothing in boost to do this. I remember a thread on comp.programming.threads about a FIFO mutex (which is what you seem to want). Someone provided an implementation (which may well do what you want), but the consensus seemed to be "don't do that". If your program behaviour depends on threads being woken in a specific order then it may be better to rewrite the code so it doesn't. Anthony -- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Is not barrier suitable for that?
Regards,
Ovanes
On Tue, May 5, 2009 at 12:42 PM, Anthony Williams
Zachary Turner
writes: I've been trying to figure out if there's a simple way using condition variables and mutexes to design a function with the following properties (cliffs notes first, and then longer version in case it's not enough info):
1) there is a section of code in the function such that two threads cannot enter that section at the same time. 2) after leaving the section of code, the thread should wait for something to happen (signalled by another thread) 3) the waits in step 2 should be awoken in the order they occured.
There's nothing in boost to do this. I remember a thread on comp.programming.threads about a FIFO mutex (which is what you seem to want). Someone provided an implementation (which may well do what you want), but the consensus seemed to be "don't do that". If your program behaviour depends on threads being woken in a specific order then it may be better to rewrite the code so it doesn't.
Anthony -- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Ovanes Markarian
On Tue, May 5, 2009 at 12:42 PM, Anthony Williams
wrote: Zachary Turner
writes: > I've been trying to figure out if there's a simple way using condition > variables and mutexes to design a function with the following properties > (cliffs notes first, and then longer version in case it's not enough info): > > 1) there is a section of code in the function such that two threads cannot > enter that section at the same time. > 2) after leaving the section of code, the thread should wait for something to > happen (signalled by another thread) > 3) the waits in step 2 should be awoken in the order they occured.
There's nothing in boost to do this. I remember a thread on comp.programming.threads about a FIFO mutex (which is what you seem to want). Someone provided an implementation (which may well do what you want), but the consensus seemed to be "don't do that". If your program behaviour depends on threads being woken in a specific order then it may be better to rewrite the code so it doesn't.
Is not barrier suitable for that?
No. Barrier forces N threads to wait until all N have reached the barrier, then releases them all. Anthony -- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
participants (3)
-
Anthony Williams
-
Ovanes Markarian
-
Zachary Turner