Solved

Why Semaphore not working ?

Posted on 2003-11-01
7
218 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WinWaitActive parameters 12 31
ShiftLeft challenge 21 81
Fibonacci challenge 11 122
Unix Command -- Challenging  question 7 90
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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