pthread_cond_wait() 总能赢得锁定互斥量的竞争吗?
Can pthread_cond_wait() always win the competition in locking a mutex?
这个问题是关于llnl中的pthread教程的。
假设有三个线程。
线程 1:
pthread_mutex_lock(&mutex)
do_something...
if condition
pthread_cond_signal(&con)
pthread_mutex_unlock(&mutex)
repeat
线程 2:
pthread_mutex_lock(&mutex)
do_something...
if condition
pthread_cond_signal(&con)
pthread_mutex_unlock(&mutex)
repeat
线程 3:
pthread_mutex_lock(&mutex)
while(condition not holds)
pthread_cond_wait(&con)
do_something...
pthread_mutex_unlock(&mutex)
假设Thread1检查条件是否满足,然后发送信号唤醒Thread3。最后它解锁互斥体。但与此同时,线程 2 正试图锁定互斥量。
我的问题是:是否可以保证Thread3永远在竞争中获胜?
如果不是那么在Thread2do_something...之后,条件变量可能会改变,那么当Thread3得到锁定互斥锁,条件变量与预期的不同。
我的问题是:有没有办法保证Tread3总能在比赛中获胜?
没有这样的保证。来自 POSIX:
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).
If more than one thread is blocked on a condition variable, the
scheduling policy shall determine the order in which threads are
unblocked. When each thread unblocked as a result of a
pthread_cond_broadcast() or pthread_cond_signal() returns from its
call to pthread_cond_wait() or pthread_cond_timedwait(), the thread
shall own the mutex with which it called pthread_cond_wait() or
pthread_cond_timedwait(). The thread(s) that are unblocked shall
contend for the mutex according to the scheduling policy (if
applicable), and as if each had called pthread_mutex_lock().
(强调我的)。
如果不是那么在Tread2 do_something...之后,条件变量可能会改变,那么当Tread3 获取互斥量锁时,条件变量与它期望的不同。
如果线程的执行顺序很重要,那么您可能需要重写代码。
无法保证。从 pthread_cond_wait()
返回应该被视为暗示条件 可能 已经改变,而不是它肯定已经 - 所以你需要重新检查条件,并且可能需要等待再次。这就是为什么应该在检查条件的循环中调用 pthread_cond_wait()
的原因。
这个问题是关于llnl中的pthread教程的。 假设有三个线程。
线程 1:
pthread_mutex_lock(&mutex)
do_something...
if condition
pthread_cond_signal(&con)
pthread_mutex_unlock(&mutex)
repeat
线程 2:
pthread_mutex_lock(&mutex)
do_something...
if condition
pthread_cond_signal(&con)
pthread_mutex_unlock(&mutex)
repeat
线程 3:
pthread_mutex_lock(&mutex)
while(condition not holds)
pthread_cond_wait(&con)
do_something...
pthread_mutex_unlock(&mutex)
假设Thread1检查条件是否满足,然后发送信号唤醒Thread3。最后它解锁互斥体。但与此同时,线程 2 正试图锁定互斥量。
我的问题是:是否可以保证Thread3永远在竞争中获胜?
如果不是那么在Thread2do_something...之后,条件变量可能会改变,那么当Thread3得到锁定互斥锁,条件变量与预期的不同。
我的问题是:有没有办法保证Tread3总能在比赛中获胜?
没有这样的保证。来自 POSIX:
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).
If more than one thread is blocked on a condition variable, the scheduling policy shall determine the order in which threads are unblocked. When each thread unblocked as a result of a pthread_cond_broadcast() or pthread_cond_signal() returns from its call to pthread_cond_wait() or pthread_cond_timedwait(), the thread shall own the mutex with which it called pthread_cond_wait() or pthread_cond_timedwait(). The thread(s) that are unblocked shall contend for the mutex according to the scheduling policy (if applicable), and as if each had called pthread_mutex_lock().
(强调我的)。
如果不是那么在Tread2 do_something...之后,条件变量可能会改变,那么当Tread3 获取互斥量锁时,条件变量与它期望的不同。
如果线程的执行顺序很重要,那么您可能需要重写代码。
无法保证。从 pthread_cond_wait()
返回应该被视为暗示条件 可能 已经改变,而不是它肯定已经 - 所以你需要重新检查条件,并且可能需要等待再次。这就是为什么应该在检查条件的循环中调用 pthread_cond_wait()
的原因。