您的位置:首页 > 其它

Spurious wakeup

2014-02-19 14:46 113 查看
Spurious wakeup现象是在条件变量使用中出现的,即一个线程可能即使没有条件变量signal的时候也会被唤醒(a thread might be awoken from its waiting state even
though no thread signaled the condition variable.)这样会导致wait线程误以为条件成立,因此,正确做法是通过while判断相应的条件,如下所示:

/* In any waiting thread: */
while(!buf->full)
wait(&buf->cond, &buf->lock);

/* In any other thread: */
if(buf->n >= buf->size){
buf->full = 1;
signal(&buf->cond);
}


参考资料:

http://en.wikipedia.org/wiki/Spurious_wakeup

https://gist.github.com/chenshuo/6430925

/article/1911641.html

http://stackoverflow.com/questions/4544234/calling-pthread-cond-signal-without-locking-mutex
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: