Solved

CSingleLock

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 101
repeatSeparator  java  challenge 13 58
Adapt this command to show who installed 29 111
Way to decrease size of apk file 9 88
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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