$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2007-11-22 03:53:16
Fabian Sturm <f <at> rtfs.org> writes:
> 
> Hi!
> 
> I have a quite simple problem, but I still lack a nice implementation in
> C++ with boost.
> 
> My problem is, that I have two threads A and B and thread A needs some
> information that B will calculate at runtime. Therefore I need some kind
> of synchronization.
> 
> So my first attempt was to use a mutex and a condition variable:
> 
> Thread A:
> 
> boost::thread::mutex::scoped_lock lock ( mMutex );
> mConditionVar.wait ( lock );
> 
> Thread B:
> 
> mConditionVar.notify_one ();
Correct way to use condvar is the following:
Thread A:
mutex::scoped_lock lock(mutex);
while (!dataCalculated)
    condvar.wait(lock);
Thread B:
mutex::scoped_lock lock(mutex);
dataCalculated = true;
condvar.notify_one();
Instead of 'while' loop you can use
condition::wait with a predicate:
Thread A:
mutex::scoped_lock lock(mutex);
condvar.wait(lock, var(dataCalculated));
Regards,
Roman Perepelitsa.