我想了解,当 thread_1 多次发送 pthread_cond_signal 到已经收到信号且仍在进行中的 thread_2(等待 pthread_cond_wait)时会发生什么。
- thread_1 会阻塞直到信号被处理吗?
- 或者该信号是否会在 thread_2 处被忽略并且 thread_1 继续执行
在上述任何一种情况下,我们是否可以获得任何错误消息,以便我们可以采取适当的操作(例如在线程1中返回适当的错误)
非常感谢。
最佳答案
I wanted to understands, what happens when a thread_1 sends pthread_cond_signal multiple times to a thread_2(waiting on pthread_cond_wait) which has already received a signal and still in progress.
你一开始就不能这样做。 pthread_cond_signal
对条件变量执行操作;它不向任何特定线程发出信号。我想你的意思是问当线程调用时会发生什么
pthread_cond_signal
,指定此时没有服务员的 CV。
- Will thread_1 block until signal is processed?
- OR Will the signal be ignored at thread_2 and thread_1 continues its execution
后者。 pthread_cond_signal
不应被解释为尝试向任何特定线程发送消息。如果有任何消息被传递,那就是内核,或者通过它传递的 CV。无论如何,如果在没有线程等待指定 CV 时执行此类调用,则它不会产生任何效果。它当然不会阻塞调用线程。
In either of above cases, can we get any error messages so that we can take appropriate action (like return appropriate error in thread1)
不,因为向没有服务员的简历发出信号并不是错误。如果您希望执行某种同步消息交换,那么您可以在互斥锁和 CV 之上执行此操作,但它们本身并不能提供您想要的功能。
关于c - pthread_cond_signal 和 pthread_cond_wait,错误检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43920706/