?
Solved

CSingleLock

Posted on 1999-01-07
3
Medium Priority
?
546 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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