I want to wait one thread of 2 thread that executed in a Simultaneous simulator until a condition has been occurred, may be the condition occurred after 1000 or more cycles of running a program in the simulator, after the condition occurred the waited thread executed again, how can I do it?
You need conditional variables.
If your compiler supports std::conditional
introduced by C++11, then you can see this for detail:
If your compiler doesn't support it, and you work with win32 threads, then see this:
And here is a complete example.
And if you work with POSIX threads, then see this:
You can see my implementation of conditional_variable
using win32 primitives here:
Scroll down and see it's implementation first, then see the usage in the concurrent queue implementation.
A typical usage of conditional variable is this:
//lock the mutex first!
scoped_lock myLock(myMutex);
//wait till a condition is met
myConditionalVariable.wait(myLock, CheckCondition);
//Execute this code only if the condition is met
whereCheckCondition
is a function (or functor) which checks the condition. It is called by wait()
function internally when it spuriously wakes up and if the condition has not met yet, the wait()
function sleeps again. Before going to sleep, wait()
releases the mutex, atomically.