虚假唤醒的返回值是什么?

3
在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.

当出现虚假唤醒时,该函数会返回还是?尽管据我所知,虚假唤醒在技术上并不被视为错误。

C++函数等效参考详细记录了虚假唤醒的影响。它还有一个重载版本来处理它们(具有等效代码,显示您应该执行的操作)。 - Sander De Dycker
1个回答

2
如果能够判断唤醒是虚假的,它就不会执行。它不会为了恶作剧而进行虚假唤醒。这种情况发生是因为在唤醒被安排之后但在被唤醒的线程成功执行任何操作之前,条件的值可能会发生变化。
由于无法判断返回值是否虚假,因此它的返回值不能反映这一事实。它只是正常的成功返回。你的第一个任务是验证条件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接