java的同步块就像windows的临界区或互斥锁:一次只能有一个线程进入。
但是有一个区别:当你在同步块内调用 wait() 时,其他线程获得进入块的能力。不知道如何在winapi中做同样的事情。
这不是解决方案:
LeaveCriticalSection();
// Something important may happen between these calls
WaitForSingleObject();
爪哇:
Thread 1 Thread 2 Thread 3
1
2 synchronized {
3 wait
5 synchronized {
6 notifyAll
7 }
4 }
8 synchronized {
9 wait
10 }
11 continue not continue
雷米的变种:
Thread 1 Thread 2 Thread 3
1 EnterCriticalSection
2 ResetEvent
3 LeaveCriticalSection
4 EnterCriticalSection
5 SetEvent
6 LeaveCriticalSection
7 EnterCriticalSection
8 ResetEvent
9 LeaveCriticalSection
10 WaitForSingleObject WaitForSingleObject
11 not continue not continue