This question concerns the pthread API for Posix systems.
这个问题涉及Posix系统的pthread API。
My understanding is that when waiting for a conditional variable, or more specifically a pthread_cond_t
, the flow goes something like this.
我的理解是,在等待条件变量,或者更具体地说是pthread_cond_t时,流程就像这样。
// imagine the mutex is named mutex and the conditional variable is named cond
// first we lock the mutex to prevent race conditions
pthread_mutex_lock(&mutex);
// then we wait for the conditional variable, releasing the mutex
pthread_cond_wait(&cond, &mutex);
// after we're done waiting we own the mutex again have to release it
pthread_mutex_unlock(&mutex);
In this example we stop waiting for the mutex when some other thread follows a procedure like this.
在这个例子中,当一些其他线程遵循这样的过程时,我们停止等待互斥锁。
// lock the mutex to prevent race conditions
pthread_mutex_lock(&mutex);
// signal the conditional variable, giving up control of the mutex
pthread_cond_signal(&cond);
My understanding is that if multiple threads are waiting some kind of scheduling policy will be applied, and whichever thread is unblocked also gets back the associated mutex.
我的理解是,如果多个线程正在等待,将应用某种调度策略,并且取消阻塞的任何线程也会返回关联的互斥锁。
Now what I don't understand is what happens when some thread calls pthread_cond_broadcast(&cond)
to awake all of the threads waiting on the conditional variable.
现在我不明白当一些线程调用pthread_cond_broadcast(&cond)来唤醒等待条件变量的所有线程时会发生什么。
Does only one thread get to own the mutex? Do I need to wait in a fundamentally different manner when waiting for a broadcast than when waiting for a signal (i.e. by not calling pthread_mutex_unlock
unless I can confirm this thread acquired the mutex)? Or am I wrong in my whole understanding of how the mutex/cond relationship works?
只有一个线程拥有互斥锁吗?在等待广播时,我是否需要以一种根本不同的方式等待,而不是等待信号(即不通过调用pthread_mutex_unlock,除非我能确认此线程获得了互斥锁)?或者我完全理解互斥/关系如何工作是错误的?
Most importantly, if (as I think is probably the case) pthread_cond_broadcast
causes threads to compete for the associated mutex as if they had all tried to lock it, does that mean only one thread will really wake up?
最重要的是,如果(我认为可能就是这种情况)pthread_cond_broadcast导致线程竞争相关的互斥锁,好像它们都试图锁定它,这是否意味着只有一个线程会真正唤醒?
1 个解决方案
#1
3
When some thread calls pthread_cond_broadcast
while holding the mutex, it holds the mutex. Which means that once pthread_cond_broadcast
returns, it still owns the mutex.
当某个线程在持有互斥锁的同时调用pthread_cond_broadcast时,它会保留互斥锁。这意味着一旦pthread_cond_broadcast返回,它仍然拥有互斥锁。
The other threads will all wake up, try to lock the mutex, then go to sleep to wait for the mutex to become available.
其他线程将全部唤醒,尝试锁定互斥锁,然后进入睡眠状态以等待互斥锁变为可用。
If you call pthread_cond_broadcast
while not holding the mutex, then one of the other threads will be able to lock the mutex immediately. All the others will have to wait to lock the mutex.
如果在未持有互斥锁的情况下调用pthread_cond_broadcast,则其他线程之一将能够立即锁定互斥锁。所有其他人将不得不等待锁定互斥锁。
#1
3
When some thread calls pthread_cond_broadcast
while holding the mutex, it holds the mutex. Which means that once pthread_cond_broadcast
returns, it still owns the mutex.
当某个线程在持有互斥锁的同时调用pthread_cond_broadcast时,它会保留互斥锁。这意味着一旦pthread_cond_broadcast返回,它仍然拥有互斥锁。
The other threads will all wake up, try to lock the mutex, then go to sleep to wait for the mutex to become available.
其他线程将全部唤醒,尝试锁定互斥锁,然后进入睡眠状态以等待互斥锁变为可用。
If you call pthread_cond_broadcast
while not holding the mutex, then one of the other threads will be able to lock the mutex immediately. All the others will have to wait to lock the mutex.
如果在未持有互斥锁的情况下调用pthread_cond_broadcast,则其他线程之一将能够立即锁定互斥锁。所有其他人将不得不等待锁定互斥锁。