Solved

CSingleLock

Posted on 1999-01-07
3
525 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:nicholash
3 Comments
 
LVL 30

Expert Comment

by:Zoppo
ID: 1327345
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
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 total points
ID: 1327346
Why are you creating the l2 with a second parameter of TRUE? In this case, the constructor for CSingleLock will block until it is able to acquire the semaphore. When the next instruction executes, the semaphore is locked already. Now you are calling Lock on it again? This is where the assertion occurs.

So you can fix it by not using CSingleLock constructor with the second parameter as TRUE. Or you can fix this problem by not calling l2.Lock().

0
 

Author Comment

by:nicholash
ID: 1327347
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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now