c - pthread_cond_signal 和 pthread_cond_wait,错误检查

标签 c pthreads

我想了解,当 thread_1 多次发送 pthread_cond_signal 到已经收到信号且仍在进行中的 thread_2(等待 pthread_cond_wait)时会发生什么。

  1. thread_1 会阻塞直到信号被处理吗?
  2. 或者该信号是否会在 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。

  1. Will thread_1 block until signal is processed?
  2. 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/

相关文章:

multithreading - 线程函数如何访问父线程的变量

c - 来自用户输入的双指数

c - 对 pthread_create 的 undefined reference

c - 传递给 pthread_create 的例程何时开始?

c - 异步返回尚未完全 "set up"(或已处理)的结果(作为结构)的最佳方法是什么

c - C 中的 pthreads - pthread_exit

在循环控制条件内调用 void 函数

c++ - 如何将 "shebang"文件的内容提供给它调用的程序?

c - 将可变大小的数组分配给固定大小的数组 c

c - 从 "the Book"开始进行 C 数组练习的直观方法。 [例1-13]