c - 虚假唤醒的返回值是多少?

标签 c multithreading posix c11

在C11中,cnd_timedwait函数定义如下:

int cnd_timedwait( cnd_t* restrict cond, mtx_t* restrict mutex,
                   const struct timespec* restrict time_point );

Atomically unlocks the mutex pointed to by mutex and blocks on the condition variable pointed to by cond until the thread is signalled by cnd_signal or cnd_broadcast, or until the TIME_UTC based time point pointed to by time_point has been reached, or until a spurious wake-up occurs. The mutex is locked again before the function returns.

Return value

thrd_success if successful, thrd_timedout if the timeout time has been reached before the mutex is locked, or thrd_error if an error occurred.

当虚假唤醒发生时,函数会返回thrd_success 还是thrd_error

尽管据我所知,虚假唤醒在技术上不被视为错误。

最佳答案

如果 cnd_timedwait 可以判断唤醒是虚假的,它就不会这样做。它不会为了欺骗您而进行虚假唤醒。它们的发生是因为条件的值可以在计划唤醒之后但在被唤醒的线程设法做任何事情之前发生变化。

由于 cnd_timedwait 无法判断返回是否虚假,因此它的返回值无法反射(reflect)这一事实。这是正常的成功返回。您的首要任务是验证条件。

关于c - 虚假唤醒的返回值是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59564596/

相关文章:

C 列表-> 数据不显示在终端上

c - 如何用 char 初始化矩阵。我正在尝试做公鸡游戏,一开始我需要一个 3x3 的矩阵,其中 "-"

c - 带有互斥锁的 Pthreads 程序 - 每次都打印相同的字符串

posix - 如果 _POSIX_VDISABLE 值为 -1 会怎样?

c++ - 从 C++ 代码调用 C 函数

c - C 中的矩阵乘法

c# - 对字节数组中的每个元素求和

java - 如何确保枚举的状态特定于一个实例

c - pthread 互斥体在等待时未解锁

c - 线程为 "suspended"对 POSIX 意味着什么?