多线程虚假唤醒 Spurious wakeup

问题描述:
线程在等待signal信号时,即使等待的条件变量并没有变化,线程仍然可能被唤醒。
解决的办法是设置一个while循环,检测条件变量是否真正改变了,如果没有就继续wait

维基百科里的描述:
Spurious wakeup describes a complication in the use of condition variables as provided by certain multithreading APIs such as POSIX Threads and the Windows API.

Even after a condition variable appears to have been signaled from a waiting thread's point of view, the condition that was awaited may still be false. One of the reasons for this is a spurious wakeup; that is, a thread might be awoken from its waiting state even though no thread signaled the condition variable. For correctness it is necessary, then, to verify that the condition is indeed true after the thread has finished waiting. Because spurious wakeup can happen repeatedly, this is achieved by waiting inside a loop that terminates when the condition is true

程序示例:

static pthread_mutex_t  my_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t   my_cond;
int my_flag = 0;
int rc = 0;

线程1:
rc = pthread_cond_init(&my_cond, (pthread_condattr_t*)NULL);
if (rc != 0)
{
    return -1;
}

pthread_mutex_lock(&my_mutex);
while(my_flag == 0)
    pthread_cond_wait(&my_cond, &my_mutex);
pthread_mutex_unlock(&my_mutex);

线程2:
rc = pthread_mutex_lock(&my_mutex);
if (rc != 0)
{
    pthread_exit(NULL);
}

my_flag = 1;

rc = pthread_cond_signal(&my_cond);
if (rc != 0)
{
    pthread_exit(NULL);
}

rc = pthread_mutex_unlock(&my_mutex);
if (rc != 0)
{
    pthread_exit(NULL);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值