Solved

Why Semaphore not working ?

Posted on 2003-11-01
7
211 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

12 Experts available now in Live!

Get 1:1 Help Now