CCriticalSection problems
-
Hi, I have few questions about CCriticalSection usage: 1) What is the purpose of the following code ?
CSomeClass::someFunc() { . . // some code . cs.Lock(); // cs is CCriticalSection instance cs.Unlock(); . . // some code . }
What's the sense ? Why it calls Lock() and immediately after it Unlock() ??? I was very surprised when I found this piece of code in MSDN... Why it isn't written this way ?CSomeClass::someFunc() { cs.Lock(); // cs is CCriticalSection instance . . . // some code . . . cs.Unlock(); }
??? 2) Is it safe to use CCriticalSection object directly, without using it together with CSingleLock ??? 3) In my program, calling CCriticalSection::Lock() sometimes blocks program execution and never returns. It just freezes the application. In which cases it happens ? Is there any way to avoid this behaviour ? Any help or suggestion will be appreciated, thanks in advance. Standa. Celebrate Mr. Cesilko! -
Hi, I have few questions about CCriticalSection usage: 1) What is the purpose of the following code ?
CSomeClass::someFunc() { . . // some code . cs.Lock(); // cs is CCriticalSection instance cs.Unlock(); . . // some code . }
What's the sense ? Why it calls Lock() and immediately after it Unlock() ??? I was very surprised when I found this piece of code in MSDN... Why it isn't written this way ?CSomeClass::someFunc() { cs.Lock(); // cs is CCriticalSection instance . . . // some code . . . cs.Unlock(); }
??? 2) Is it safe to use CCriticalSection object directly, without using it together with CSingleLock ??? 3) In my program, calling CCriticalSection::Lock() sometimes blocks program execution and never returns. It just freezes the application. In which cases it happens ? Is there any way to avoid this behaviour ? Any help or suggestion will be appreciated, thanks in advance. Standa. Celebrate Mr. Cesilko!It has been sometime since I dealt with this, so please forgive the vagness. Anyway, I think you need to declare the CS like this volatile CCriticalSection cs; otherwise the compiler will wrongly optimize the cs itself. If this does not work you may need to volatile BOOL bHasLock; and test bHasLock before Locking and Unlocking. It has been a long time, I just do not remember. But the voliatile keyword is a must to prevent the compiler optimize problems. Phil F