I'm working as a support engineer, and in the dump of a failing process, I see that there are some locks.
Using Windbg (Visual Studio is not able to handle the call stacks correctly) I've found that one function (ClassName::F()
) is coming back. That function uses a critical section and calls for a subfunction (ClassName::f_sub()
), who's calling the same critical section, in a nutshell:
int ClassName::f_sub(){
EnterCriticalSection(&m_cs);
...
LeaveCriticalSection(&m_cs);
return ...;
}
int ClassName::F() {
EnterCriticalSection(&m_cs);
...
int temp = f_sub();
...
LeaveCriticalSection(&m_cs);
return ...;
}
Every time it's the same critical section m_cs
(a property of ClassName
) being used.
In my opinion, this makes following sequence possible:
Thread 1 : F() : Enter the critical section. (Thread 1 is in)
Thread 1 : f_sub() : Enter the critical section. (Thread 1 is in)
Thread 1 : f_sub() : Leave the critical section. (Thread 1 is out)
Thread 2 : F() : Enter the critical section. (Thread 2 is in)
=> WRONG! Thread 2 should be forced to wait for Thread 1 leaving the critical section via F().
Is my analysis correct and does this mean that it's advised to have different critical sections for main and sub functions?