C++ 理解 pthread_cond_wait() 和 pthread_cond_signal()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16522858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
understanding of pthread_cond_wait() and pthread_cond_signal()
提问by user1944267
Generally speaking, pthread_cond_wait()
and pthread_cond_signal()
are called as below:
一般来说,pthread_cond_wait()
并pthread_cond_signal()
称为如下:
//thread 1:
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
do_something()
pthread_mutex_unlock(&mutex);
//thread 2:
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
The steps are
步骤是
pthread_cond_wait(&cond, &mutex);
is called, it unlocks the mutexThread 2 locks the mutex and calls
pthread_cond_signal()
, which unlocks the mutexIn thread 1,
pthread_cond_wait()
is called and locks the mutex again
pthread_cond_wait(&cond, &mutex);
被调用,它解锁互斥锁线程 2 锁定互斥锁并调用
pthread_cond_signal()
,从而解锁互斥锁在线程 1 中,
pthread_cond_wait()
被调用并再次锁定互斥锁
Now in thread 2, after pthread_cond_signal()
is called, pthread_mutex_unlock(&mutex)
is going to run, it seems to me that it wants to unlock a the mutex which is now locked by thread 1. Is there anything wrong in my understanding?
现在在线程 2 中,在pthread_cond_signal()
被调用之后, pthread_mutex_unlock(&mutex)
将要运行,在我看来它想要解锁一个现在被线程 1 锁定的互斥锁。我的理解有什么问题吗?
Besides, it also seems to me that pthread_cond_wait()
can be called by only 1 thread for the same cond-mutex pair. But there is a saying "The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond)." So, it means pthread_cond_wait()
can be called by many threads for the same cond-mutex pair?
此外,在我看来,pthread_cond_wait()
对于同一个 cond-mutex 对,只能由 1 个线程调用。但是有一种说法“pthread_cond_signal() 函数应至少解除阻塞在指定条件变量 cond 上的线程中的一个(如果有任何线程在 cond 上被阻塞)。” 那么,这意味着pthread_cond_wait()
可以由多个线程为同一个 cond-mutex 对调用?
回答by Chris Dodd
pthread_cond_signal
does not unlock the mutex (it can't as it has no reference to the mutex, so how could it know what to unlock?) In fact, the signal need not have any connection to the mutex; the signalling thread does not need to hold the mutex, though for most algorithms based on condition variables it will.
pthread_cond_signal
不解锁互斥锁(它不能,因为它没有对互斥锁的引用,所以它怎么知道要解锁什么?)实际上,信号不需要与互斥锁有任何连接;信号线程不需要持有互斥锁,但对于大多数基于条件变量的算法来说,它会。
pthread_cond_wait
unlocks the mutex just before it sleeps (as you note), but then it reaquires the mutex (which may require waiting) when it is signalled, before it wakes up. So if the signalling thread holds the mutex (the usual case), the waiting thread will not proceed until the signalling thread also unlocks the mutex.
pthread_cond_wait
在它睡觉之前解锁互斥锁(如您所见),但是当它被发出信号时,它会在它醒来之前重新获取互斥锁(这可能需要等待)。因此,如果信号线程持有互斥锁(通常情况下),则等待线程将不会继续,直到信号线程也解锁互斥锁。
The common use of condition vars is something like:
条件变量的常见用法是这样的:
thread 1:
pthread_mutex_lock(&mutex);
while (!condition)
pthread_cond_wait(&cond, &mutex);
/* do something that requires holding the mutex and condition is true */
pthread_mutex_unlock(&mutex);
thread2:
pthread_mutex_lock(&mutex);
/* do something that might make condition true */
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
The two threads have some shared data structure that the mutex is protecting access to. The first thread wants to wait until some condition is true then immediately do some operation (with no race condition opportunity for some other thread to come in between the condition check and action and make the condition false.) The second thread is doing something that might make the condition true, so it needs to wake up anyone that might be waiting for it.
这两个线程有一些共享数据结构,互斥锁保护访问。第一个线程想要等到某个条件为真,然后立即执行一些操作(没有其他线程在条件检查和操作之间进入并使条件为假的竞争条件机会。)第二个线程正在做一些可能使条件为真,因此它需要唤醒可能正在等待它的任何人。
回答by Ludzu
Here is a typical example: thread 1 is waiting for a condition, which may be fulfilled by thread 2.
这是一个典型的例子:线程 1 正在等待一个可能由线程 2 满足的条件。
We use one mutex and one condition.
我们使用一个互斥锁和一个条件。
pthread_mutex_t mutex;
pthread_cond_t condition;
thread 1 :
线程 1:
pthread_mutex_lock(&mutex); //mutex lock
while(!condition){
pthread_cond_wait(&condition, &mutex); //wait for the condition
}
/* do what you want */
pthread_mutex_unlock(&mutex);
thread 2:
线程2:
pthread_mutex_lock(&mutex);
/* do something that may fulfill the condition */
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&condition); //wake up thread 1
Edit
编辑
As you can see in the pthread_cond_waitmanual:
正如您在pthread_cond_wait手册中所见:
It atomically releases mutex and causes the calling thread to block on the condition variable cond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable".
它以原子方式释放互斥锁并导致调用线程在条件变量 cond 上阻塞;原子地这里的意思是“原子地相对于另一个线程访问互斥锁然后访问条件变量”。