Why does this code cause a deadlock?
THREAD 1:
EnterCriticalSection( &lock_ );
... Create thread 1
EnterCriticalSection( &lock_ );
while (pred) {
SleepConditionVariableCs( &cond_, &lock_ );
// At this point, I would expect thread #2 to wake up, but it doesn't.
}
LeaveCriticalSection( &lock_ );
LeaveCriticalSEction( &lock_ );
THREAD 2:
EnterCriticalSection( &lock_ );
// This never runs
... Do something else for a while
LeaveCriticalSection( &lock_ );
According to the Win32 API, EnterCriticalSection can be called twice in a row from the same thread without deadlocking. It appears that SleepConditionVariableCS only unlocks the critical section once, which means that thread #2 will never run. Is my reasoning correct here?
Basically, what I want is something like Java’s ReentrantLock. What’s the difference between ReentrantLock and CRITICAL_SECTION?
It appears that SleepConditionVariableCS only unlocks the critical section once, which means that thread #2 will never run.