Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

waiting thread until a condition has been occurred

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?

like image 584
Angelia Wikin Avatar asked Jun 11 '12 05:06

Angelia Wikin


People also ask

How do you make a thread wait for a condition?

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.

What happens when a thread is waiting?

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.

How do you make a thread wait in C++?

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.

Does condition variable wait lock?

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.


2 Answers

You need conditional variables.

If your compiler supports std::conditional introduced by C++11, then you can see this for detail:

  • std::condition_variable (C++11 threads)

If your compiler doesn't support it, and you work with win32 threads, then see this:

  • Condition Variables (Win32 threads)

And here is a complete example.

And if you work with POSIX threads, then see this:

  • Condition Variables (POSIX threads)

You can see my implementation of conditional_variable using win32 primitives here:

  • Implementation of concurrent blocking queue for producer-consumer

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.

like image 55
Nawaz Avatar answered Sep 30 '22 15:09

Nawaz


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:

  • acquire mutex
  • while PREDICATE is not true
    • wait on condition variable
  • do work on critical section
  • if PREDICATE is true
    • signal condition variable
  • release mutex

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.

  • acquire mutex
  • do work on critical section
  • if PREDICATE is true
    • signal condition variable
  • release mutex

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() 
like image 33
jxh Avatar answered Sep 30 '22 17:09

jxh