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?
Explanation: When you want to sleep a thread, condition variable can be used. In C under Linux, there is a function pthread_cond_wait() to wait or sleep. On the other hand, there is a function pthread_cond_signal() to wake up sleeping or waiting thread. Threads can wait on a condition variable.
A thread is in the waiting state when it wants to wait on a signal from another thread before proceeding. Once this signal is received, it becomes runnable. A thread moves to the blocked state when it wants to access an object that is being used (locked) by another thread.
To wait for a thread use the std::thread::join() function. This function makes the current thread wait until the thread identified by *this has finished executing. A C++ program is given below.
Condition variables: used to wait for a particular condition to become true (e.g. characters in buffer). wait(condition, lock): release lock, put thread to sleep until condition is signaled; when thread wakes up again, re-acquire lock before returning.
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.
If you don't have C++11, but you do have a system that supports POSIX threads, then you can use a condition variable. There are other choices, but a condition variable may be the most straight forward given the way you have described your problem.
A pthread condition variable is used in conjunction with a mutex. The trick with the condition variable is that waiting on it causes the acquired mutex to be released, until the wait call returns, at which point the mutex has been acquired again. The sequence is:
The signal step is used in case multiple threads are entering the same critical section above.
If a different thread may access the same mutex to modify state that affects the PREDICATE, that thread should check to see if anyone needs to be signaled.
The POSIX commands of interest are:
pthread_mutex_init() pthread_mutex_destroy() pthread_mutex_lock() pthread_mutex_unlock() pthread_cond_init() pthread_cond_destroy() pthread_cond_wait() pthread_cond_signal()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With