Link to home
Start Free TrialLog in
Avatar of nicholash
nicholash

asked on

CSingleLock

I am using a CSemaphore object with a count of 1. I use the CSingleLock object to provide the access control. I have two tasks which I am synchronising. I lock the semaphore with the first task using CSingleLock::Lock and then do the same in the second task with an INFINITE wait so that when the first task then uses Unlock to unlock the semaphore the second task can procede in sync with the first task. My problem is that in a DEBUG release I get an ASSERT because I am trying to lock a semaphore that is already locked. But this does not make sense to me because that is what semaphores are for are they not ? Or am I being stupid ?

Help would be greatly appreciated.

p.s. I have tried FAQ on Dejanews but there seems to be no official answer.
Avatar of Zoppo
Zoppo
Flag of Germany image

I only get an ASSERT when I try to lock a CSingleLock-object which is initially locked:

CSemaphor sem;
CSingleLock l1( &sem, FALSE );   // not locked
CSingleLock l2( &sem, TRUE );     // locked
l1.Lock();   // OK
l2.Lock();   // ASSERT in mtex.cpp, line 107

If you are using MSVC++ 4.0 or less, you should see MSDN Artikel Nr. Q141533 about a
known bug in MFC's CSyncObject::Lock().

hope that helps,

ZOPPO
ASKER CERTIFIED SOLUTION
Avatar of Tommy Hui
Tommy Hui

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nicholash
nicholash

ASKER

A reference to a book explaining the correct usage of CSemaphores and CSingleLock would be good. Microsoft's Help does not explain the use of sync and mutex classes very well.