|
Boost Users : |
From: Fabian Sturm (f_at_[hidden])
Date: 2007-11-21 17:25:36
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 ();
This works nice as long as thread A already started waiting on the
condition before thread B finished providing it. Otherwise the notify
will be lost.
So my second attempt was to use a barrier:
Init barrier with count 2
Thread A:
mBarrier.wait ();
Thread B:
mBarrier.wait ();
This works fine and A will wait until the result is available and it
doens't matter if it starts waiting before or after B finished
calculaating.
But the problem now is that B also needs to wait for A to reach the
point in execution.
So if I just want to signal thread A from thread B that some point was
reached. I don't know how to do it with boost.
If I would have a semaphore available I could init it with 0 and
thread A tries to decrement it while thread B will increment it once
it reached the point.
But http://www.boost.org/doc/html/thread/faq.html#id1747096 says:
"Semaphore was removed as too error prone.
The same effect can be achieved with greater safety by the combination
of a mutex and a condition variable."
So how exactly is this achieved with the mutex and condition variable?
I always only come up with a mutex, condition variable and a bool which
all three must be shared between both threads.
I would greatly appreciate any suggestions!
Thanks, Fabian
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