Solved

Why Semaphore not working ?

Posted on 2003-11-01
7
222 Views
Last Modified: 2013-11-20
Hi,
   I have written a simple Dialog Based application in VC++. I have added one class to it named CMyResource.
There are 8 Instances of my resource and I want to allow only 4 at a time and concurrently . I believe this suggests
use of semaphores. So In my main dialog class
I have created instance of CSemaphore and Used
CSingleLock to point to it. Initial count for semaphore
Have kept to 4 ie
CSemaphore mySemaphore(1,4); and
CSingleLock myLock(&mySemaphore);

I have used Afxbegin thread to create threads for all 8
instances But somehow it is not working. All 8 instances
are getting simultaneous access.

I checked the condition by

if(myLock.lock())
{

//Access resource

}
else
{

//Report Access Denied


}

Can anybody tell me why is it not working ?

Any help appreciated..

Thanks
p10m




 
 
0
Comment
Question by:p10m
  • 3
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9662690
myLock.lock();

This line gets access to semaphore immidiately, if it is free. If not, it waits untill semaphore is released. else branch is never called. All your threads get access to resource, but not simultenously.
To see that it really works, add MessageBox call to each thread. MessageBox stops thread and waits for OK click. All this time resource is locked. If semaphore works as expected, you will not see more than 4 message boxes simultemously.

By the way, I think you should create the semafore with initial count 0:

CSemaphore mySemaphore(0,4);
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9662696
Additional explanation:

myLock.lock() - returns only TRUE, because it's parameter DWORD dwTimeOut  has default value INFINITE, that means - wait until the object is signaled before returning.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 120 total points
ID: 9662886
Actually, it should be
CSemaphore mySemaphore(4, 4);

Take a look at this code:

class CSample3Dlg : public CDialog
{
    ...
    CSemaphore* m_pSemaphore;
    ...
}

BOOL CSample3Dlg::OnInitDialog()
{
    ....
    m_pSemaphore = new CSemaphore(4, 4);
    ....
}

UINT ThreadProc(LPVOID pParam)
{
    CSample3Dlg* pDlg = (CSample3Dlg*) pParam;

    CSingleLock lock(pDlg->m_pSemaphore);

    if ( lock.Lock() )
    {
        MessageBox(NULL, _T("OK"), NULL, MB_OK);
    }

    return 0;
}

void CSample3Dlg::OnButton1()
{
    for ( int i = 0; i < 8; i++ )
        AfxBeginThread(ThreadProc, this);
}

Running this code you will never see more than 4 message boxes at the same time.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9664317
Don't forget that the mutex object needs to be either a global variable or a static member of the class ... otherwise, each instance of the class will have it's own copy!

-- Dan
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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. …
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 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