调用 pthread_cond_signal 而不锁定互斥锁
我在某处读到,我们应该在调用 pthread_cond_signal 之前锁定互斥体,并在调用后解锁互斥体:
pthread_cond_signal() 例程是 用于向另一个人发出信号(或唤醒) 正在等待的线程 条件变量。应该是 互斥体锁定后调用,并且必须 解锁互斥体以便 pthread_cond_wait() 例程 完成。
我的问题是:在不锁定互斥体的情况下调用 pthread_cond_signal 或 pthread_cond_broadcast 方法不是可以吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不锁定更改条件和信号的代码路径中的互斥锁,则可能会丢失唤醒。考虑这对进程:
进程 A:
进程 B(不正确):
然后考虑这种可能的指令交错,其中
condition
开始为 < code>FALSE:条件
现在为TRUE
,但进程 A 一直在等待条件变量 - 它错过了唤醒信号。如果我们改变进程 B 来锁定互斥体:进程 B(正确):
...那么上述情况就不会发生;唤醒永远不会错过。
(请注意,实际上您可以将
pthread_cond_signal()
本身移到pthread_mutex_unlock()
之后,但这可能会导致线程调度效果不佳,并且由于条件本身的更改,您必须锁定此代码路径中已经存在的互斥体)。If you do not lock the mutex in the codepath that changes the condition and signals, you can lose wakeups. Consider this pair of processes:
Process A:
Process B (incorrect):
Then consider this possible interleaving of instructions, where
condition
starts out asFALSE
:The
condition
is nowTRUE
, but Process A is stuck waiting on the condition variable - it missed the wakeup signal. If we alter Process B to lock the mutex:Process B (correct):
...then the above cannot occur; the wakeup will never be missed.
(Note that you can actually move the
pthread_cond_signal()
itself after thepthread_mutex_unlock()
, but this can result in less optimal scheduling of threads, and you've necessarily locked the mutex already in this code path due to changing the condition itself).根据本手册:
Dave Butenhof(使用 POSIX 线程进行编程)位于 comp.programming.threads 上,并且可以使用 此处。
According to this manual :
The meaning of the predictable scheduling behavior statement was explained by Dave Butenhof (author of Programming with POSIX Threads) on comp.programming.threads and is available here.
caf,在您的示例代码中,进程 B 修改
condition
而不首先锁定互斥体。如果进程 B 在修改期间只是锁定互斥体,然后在调用 pthread_cond_signal 之前仍然解锁互斥体,那么就不会有问题 --- 我的说法正确吗?我凭直觉相信 caf 的位置是正确的:在不拥有互斥锁的情况下调用 pthread_cond_signal 是一个坏主意。但咖啡馆的例子实际上并不是支持这一立场的证据;这只是支持更弱(实际上不言而喻)的立场的证据,即除非您首先锁定了该互斥体,否则修改由互斥体保护的共享状态是一个坏主意。
任何人都可以提供一些示例代码,其中调用
pthread_cond_signal
后跟pthread_mutex_unlock
会产生正确的行为,但调用pthread_mutex_unlock
后跟pthread_cond_signal
> 产生不正确的行为?caf, in your sample code, Process B modifies
condition
without locking the mutex first. If Process B simply locked the mutex during that modification, and then still unlocked the mutex before callingpthread_cond_signal
, there would be no problem --- am I right about that?I believe intuitively that caf's position is correct: calling
pthread_cond_signal
without owning the mutex lock is a Bad Idea. But caf's example is not actually evidence in support of this position; it's simply evidence in support of the much weaker (practically self-evident) position that it is a Bad Idea to modify shared state protected by a mutex unless you have locked that mutex first.Can anyone provide some sample code in which calling
pthread_cond_signal
followed bypthread_mutex_unlock
yields correct behavior, but callingpthread_mutex_unlock
followed bypthread_cond_signal
yields incorrect behavior?