Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

condition variable [closed]

What are the principles of a condition variable in synchronization of the processes of operating systems?

like image 318
likeIT Avatar asked Apr 19 '11 12:04

likeIT


People also ask

Do you need to lock a condition variable?

A condition variable is more primitive, only providing the signal. At least in C++, there is no need to protect the signalling with a lock. In fact, depending on how the implementation handles the scheduling of the waiting thread, it might be harmful for performance to perform the signalling inside the lock.

What is a conditional variable?

A conditional variable in operating system programming is a special kind of variable that is used to determine if a certain condition has been met or not. It is used to communicate between threads when certain conditions become true. A conditional variable is like a queue.

What is condition variable in C?

A condition variable is an explicit queue that threads can put themselves on when some state of execution (i.e., some condition) is not as desired (by waiting on the condition); some other thread, when it changes said state, can then wake one (or more) of those waiting threads and thus allow them to continue (by sig- ...

What does condition variable wait do?

condition_variable::wait wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied (bool(stop_waiting()) == true).


1 Answers

Well, conditional variables allow you to wait for certain condition to occur. In practice your thread may sleep on conditional variable and other thread wakes it up.

Conditional variable also usually comes with mutex. This allows you to solve following synchronisation problem: how can you check state of some mutex protected data structure and then wait until it's state changes to something else. I.e.

/* in thread 1 */
pthread_mutex_lock(mx); /* protecting state access */
while (state != GOOD) {
    pthread_mutex_unlock(mx);
    wait_for_event();
    pthread_mutex_lock(mx);
}
pthread_mutex_unlock(mx);


/* in thread 2 */
pthread_mutex_lock(mx); /* protecting state access */
state = GOOD;
pthread_mutex_unlock(mx);
signal_event(); /* expecting to wake thread 1 up */

This pseudocode sample carries a bug. What happens if scheduler decides to switch context from thread 1 to thread 2 after pthread_mutex_unlock(mx), but before wait_for_event(). In this case, thread 2 will not wake thread 1 and thread 1 will continue sleeping, possibly forever.

Conditional variable solves this by atomically unlocking the mutex before sleeping and atomically locking it after waking up. Code that works looks like this:

/* in thread 1 */
pthread_mutex_lock(mx); /* protecting state access */
while (state != GOOD) {
    pthread_cond_wait(cond, mx); /* unlocks the mutex and sleeps, then locks it back */
}
pthread_mutex_unlock(mx);

/* in thread 2 */
pthread_mutex_lock(mx); /* protecting state access */
state = GOOD;
pthread_cond_signal(cond); /* expecting to wake thread 1 up */
pthread_mutex_unlock(mx);

Hope it helps.

like image 112
Alexander Sandler Avatar answered Oct 07 '22 10:10

Alexander Sandler