Solved

CSingleLock

Posted on 1999-01-07
3
527 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 31

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 94
cat dog challenge 18 124
Separate files macro - Errors on larger data set 18 65
"Black Box" Testing of Control System Software 2 26
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

22 Experts available now in Live!

Get 1:1 Help Now