Boost logo

Boost Users :

From: Zeljko Vrba (zvrba_at_[hidden])
Date: 2007-06-18 12:15:11


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.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net