Re: New to CSingleLock
(see in-line comments below)
"Pat" <email@example.com> wrote in message
1) Do I need a CSingleLock, or is CCriticalSection enough by itself?
CCriticalSection can be used by itself. Although, I prefer to use
CSingleLock (see below)
2) Why do both classes have Lock() and Unlock() methods?
Because you can use either.
3) (Assuming I need CSingleLock...) Do I need to call
CSingleLock::Lock() explicitly, or does just creating the CSingleLock
object perform the lock?
You do not need to call Lock, the creation of CSingleLock has an extra
parameter to signal "Gain Lock"
4) Do I need to call CSingleLock::IsLocked() before entering the
"critical" code sections, or will just acquiring the lock cause the
necessary wait to happen?
5) What happens if an exception is thrown in a "locked" section?
Excellent question. This is a great reason to use CSingleLock. CSingleLock
is an object, which when destroyed will automatically unlock. This is why
CSingleLock is great, you never have to remember to specifically call
My typical code looks like:
//Add the log
CSingleLock sl(&m_csLogs, TRUE);
m_ToWrite += pString; // add this message to the "Current messages"
The above code will wait at the creation of the CSingleLock until it has
gained the lock. The lock is automatically released/unlocked at the close
brace (the object leaves scope and is destroyed).