Critical Sections
-
Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }
-
Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }
Why are you entering and exitting the critical section twice? Critical sections are designed for exclusive entry into a location with threads, similar to a mutex, but the command issued to the cpu is a lock memory routine not a mutex counter resulting in slightly different behavior at the CPU level. I would have to compile to see what the cpu would think of entering and exitting twice, but I would recommend against it, and even if it "were" to work, you would have gained nothing over entering once. Use it like this: thread1() { EnterCriticalSection(&cs); // protected code here LeaveCriticalSection(&cs); } main() { EnterCriticalSection(&cs); // more protected code here LeaveCriticalSection(&cs); }
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }
-
Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }
This won't deadlock but I don't see the reason at all for entering your critical section twice. It won't deadlock because of that (from MSDN):
After a thread has ownership of a critical section, it can make additional calls to EnterCriticalSection or TryEnterCriticalSection without blocking its execution. This prevents a thread from deadlocking itself while waiting for a critical section that it already owns. The thread enters the critical section each time EnterCriticalSection and TryEnterCriticalSection succeed. A thread must call LeaveCriticalSection once for each time that it entered the critical section.
Cédric Moonen Software developer
Charting control [v1.2] -
Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }
tom groezer wrote:
Is there ne problem in the code below:-
Such as:* No definition for
Critical_Section
.-
main()
does not return a value.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-