boost::thread flush()?
Is there a method to release all threads waiting on a mutex? Or, more generally, a portable way to release a bunch of threads at the same time? (boost::asio?) I didn't see anything in the docs, besides barrier. Unfortunately, I can't guarantee the N threads will be waiting when a flush is needed. thanks, graham
On Mon, Jun 18, 2007 at 10:48:14AM -0500, Graham Reitz wrote:
Is there a method to release all threads waiting on a mutex? Or, more generally, a portable way to release a bunch of threads at the same time? (boost::asio?)
I didn't see anything in the docs, besides barrier. Unfortunately, I can't guarantee the N threads will be waiting when a flush is needed.
You want a SYSV-style semaphore (semget/semop/semctl in POSIX; seems not to be provided by Boost.Thread). Given N threads: 1. Initialize the semaphore to 0. 2. In every thread that wants to "lock", try to subtract 1 from the semaphore. Since this would cause the semaphore to become negative, the thread will sleep. 3. When you want to release all the threads, set the semaphore value to N. The K threads that may already be waiting on the semaphore shall be unblocked and the semaphore value will be decremented to N-K after they all wake up. The remaining N-K threads which will subsequently try to lock the semaphore will always succeed in decrementing it by 1, and will be again allowed to proceed.
Zeljko Vrba wrote:
On Mon, Jun 18, 2007 at 10:48:14AM -0500, Graham Reitz wrote:
Is there a method to release all threads waiting on a mutex? Or, more generally, a portable way to release a bunch of threads at the same time? (boost::asio?)
I didn't see anything in the docs, besides barrier. Unfortunately, I can't guarantee the N threads will be waiting when a flush is needed.
You want a SYSV-style semaphore (semget/semop/semctl in POSIX; seems not to be provided by Boost.Thread).
Not necessarily. It depends on whether we want a thread that reaches the synchronization point after the "go" to pass (as when downing a semaphore) or to stop (as with a missed condition notification). Judging from the OP, the Semaphore-approach seems a bit fragile, because if a thread misses the "go" twice, another thread won't stop as expected (because a down is missing). Generally, semaphores are very powerful and just as error prone. That's why higher-level synchronization primitives should be preferred, if available. Regards, Tobias
Excellent and thanks! I completely missed boost:condition.
Graham
On 6/18/07, Tobias Schwinger
Zeljko Vrba wrote:
On Mon, Jun 18, 2007 at 10:48:14AM -0500, Graham Reitz wrote:
Is there a method to release all threads waiting on a mutex? Or, more generally, a portable way to release a bunch of threads at the same time? (boost::asio?)
I didn't see anything in the docs, besides barrier. Unfortunately, I can't guarantee the N threads will be waiting when a flush is needed.
You want a SYSV-style semaphore (semget/semop/semctl in POSIX; seems not to be provided by Boost.Thread).
Not necessarily. It depends on whether we want a thread that reaches the synchronization point after the "go" to pass (as when downing a semaphore) or to stop (as with a missed condition notification).
Judging from the OP, the Semaphore-approach seems a bit fragile, because if a thread misses the "go" twice, another thread won't stop as expected (because a down is missing).
Generally, semaphores are very powerful and just as error prone. That's why higher-level synchronization primitives should be preferred, if available.
Regards, Tobias
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Graham Reitz wrote:
Is there a method to release all threads waiting on a mutex?
Yes.
Or, more generally, a portable way to release a bunch of threads at the same time?
Try condition::notify_all (see attached code for a sample).
Regards,
Tobias
#include <string>
#include <iostream>
#include
participants (3)
-
Graham Reitz
-
Tobias Schwinger
-
Zeljko Vrba