看到各种锁定相关的问题和(几乎)总是找到’循环因为虚假的唤醒’术语1我想知道,有没有人经历过这样的唤醒(假设一个像样的硬件/软件环境)?
我知道“虚假”一词意味着没有明显的理由,但这种事件的原因是什么?
(1注意:我不是在质疑循环练习.)
编辑:一个帮助问题(对于那些喜欢代码示例的人):
如果我有以下程序,我运行它:
public class Spurious {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition cond = lock.newCondition();
lock.lock();
try {
try {
cond.await();
System.out.println("Spurious wakeup!");
} catch (InterruptedException ex) {
System.out.println("Just a regular interrupt.");
}
} finally {
lock.unlock();
}
}
}
如果不等待一个随机事件,我可以做些什么来唤醒它等待虚假等待?
解决方法:
*article on spurious wakeups有这个小窍门:
The
pthread_cond_wait()
function in Linux is implemented using thefutex
system call. Each blocking system call on Linux returns abruptly withEINTR
when the process receives a signal. …pthread_cond_wait()
can’t restart the waiting because it may miss a real wakeup in the little time it was outside thefutex
system call. This race condition can only be avoided by the caller checking for an invariant. A POSIX signal will therefore generate a spurious wakeup.
简介:如果发出一个Linux进程的信号,它的等待线程将各自享受一个不错的,热的虚假唤醒.
我买它.这是一个比通常模糊的“性能”原因更容易吞下的药丸.