Boost logo

Boost Users :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2005-03-21 10:40:23


On Mar 19, 2005, at 1:29 PM, Leon Mergen wrote:

> Hello,
>
> I'm currently trying to achieve the following:
>
> A series of threads handle jobs (threads A). Of each of these jobs, in
> the end, a statistics object is placed in a container. This container
> will later be read-from by another thread (thread B), dedicated to
> continuously reading statistics data from other threads, and parse
> those
> statistics.
>
> Now, since it is very well possible a thread handles at least 1,000
> jobs
> before statistics data is actually read from, I want it to hold a mutex
> lock to the data at all time. The class also has an internal volatile
> boolean, which flags whether another thread (thread B) wants to read
> from the container. If so, the writing thread (threads A) releases the
> lock, after which thread B will pick up the lock and write the data and
> resets the boolean flag. After releasing the lock, threads A will
> immediately try to re-acquire the lock.
>
> However, as far as I can see, for this approach, a scoped_lock will not
> be the most fantastic solution ? Or am I missing something really
> crucial about the scoped_lock being able to hold a lock outside a
> certain scope ? :)
>
> Thanks in advance for any suggestions about a locking type... :)

This sounds like a good application for condition variables:

http://www.boost.org/doc/html/condition.html

-Howard


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