Boost logo

Boost :

From: Matt Hurd (matt.hurd_at_[hidden])
Date: 2005-09-14 19:43:39


> On 15/09/05, Roland Schwarz <roland.schwarz_at_[hidden]> wrote:

> Thank you,
> Roland
>
> #include <iostream>
>
> #include <boost/thread.hpp>
>
> using namespace boost;
>
> mutex guard;
> typedef mutex::scoped_lock lock_type;
> condition cond;
>
> long global_count = 0;
> long count_for_test = 10000;
>
> long global_number_of_threads = 0;
> long number_of_threads_for_test = 1000;
>
> bool global_flag = true;
>
> void do_work()
> {
> lock_type lock(guard);
> ++global_number_of_threads;
> cond.notify_one();
> while (global_count < count_for_test)
> cond.wait(lock);
> }
>
> void do_flag()
> {
> lock_type lock(guard);
> ++global_number_of_threads;
> cond.notify_one();
> while (global_count < count_for_test) {
> cond.wait(lock);
> global_flag = false;
> // Step 3: The following signal occasionally
> // is beeing lost. See below.
> cond.notify_one();
> }
> }
>
> int main()
> {
>
> thread_group pool;
>
> for (long i=0; i < number_of_threads_for_test; ++i)
> {
> pool.create_thread( do_work );
> }
> pool.create_thread( do_flag );
>
> { // we wait until all threads are up
> lock_type lock(guard);
> while (global_number_of_threads <
> number_of_threads_for_test+1)
> cond.wait(lock);
> }

I can't see why you get here? I might be missing something but you're
likely to end up with all threads waiting on conditions here... All
the do_work threads are waiting, the do_flag is waiting. Then if all
the threads aren't up and running we, the main thread, wait on the
condition too, but we might not be the one the consumes a notify_one
as the others threads start so we never progress. I could have the
logic arse about though...

matt.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk