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
http://blog.csdn.net/solstice/article/details/11432817
http://stackoverflow.com/questions/4544234/calling-pthread-cond-signal-without-locking-mutex