/* 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 */
/* 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);
希望有帮助。
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);
发布评论
评论(1)
好吧,条件变量允许您等待特定条件的发生。实际上,您的线程可能会在条件变量上休眠,而其他线程会将其唤醒。
条件变量通常也带有互斥体。这允许您解决以下同步问题:如何检查某些互斥锁保护的数据结构的状态,然后等待它的状态更改为其他状态。即
这个伪代码示例带有一个错误。如果调度程序决定在 pthread_mutex_unlock(mx) 之后但在 wait_for_event() 之前将上下文从线程 1 切换到线程 2,会发生什么情况。在这种情况下,线程 2 将不会唤醒线程 1,并且线程 1 将继续休眠,可能会永远休眠。
条件变量通过在睡眠前自动解锁互斥锁并在唤醒后自动锁定互斥锁来解决此问题。有效的代码如下所示:
希望有帮助。
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.
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:
Hope it helps.