Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Mutex's

Status
Not open for further replies.

sweep123

Technical User
May 1, 2003
185
0
0
GB
Can someone tell me if I have got this schem correct for usin Mutexs for controlling access to shared data (memory mapped) between two programs.

Each program creates the Mutex as follows:-

m_hMutex = ::CreateMutex(NULL,FALSE,"CSLSharedMemLock");
Each program uses the following procedure before read/write operations:-

bool WaitForMutex(void)
{
/* This procedure will wait for a Mutex before allowing any
write access to the shared area. */

if(m_hMutex)
{
if :):WaitForSingleObject(m_hMutex,INFINITE) == WAIT_OBJECT_0)
{
return true;
}
return false;
}
then we read data or write data and release the handle.
ReleaseMutex(m_hMutex);
and then

CloseHandle (m_hMutex);
But one of the programs is a DLL and that does not call

CloseHandle (m_hMutex);

The 2 programs (EXE and DLL) work OK without using the Mutex, but lock up as soon as I start to use a Mutex; i.e. never return from WaitForMutex.

I want to protect the shared data, any comments?

Sweep123.



 
Once you call CreateMutex you get a handle back but if the mutex already existed, you dont create one. First check if it already exists by calling GetLastError. Wait for single object IF it already existed. If you wait for it in the code that created it, you will enter deadlock.

Been a while since I used mutexes but I think I did something along the following (see below)

Matt

Code:
m_hMutex = CreateMutex(NULL,FALSE,"CSLSharedMemLock");
ASSERT(m_hMutex);
if(::GetLastError() == ERROR_ALREADY_EXISTS || ::GetLastError() == ERROR_ACCESS_DENIED)
{
   WaitForSingleObject(hMutex,INFINITE);
   m_hMutex = CreateMutex(NULL,FALSE,"CSLSharedMemLock");
   
   // if get last error shows that it exists, we were too slow
   // possibly make a recursive call???  Maybe put this in a loop?

}
 
Zyrenthian~~

You should be careful about that code...
You use CreateMutex with bInitialOwner set to FALSE (second parameter).

That means that the thread/process that created the mutex does not own it, you should be calling for WaitForSingleObject to actually own it.
Otherwise, if you return from the mutex-creation function without doing that, any other thread will get the mutex instead.

At least, this is my perception of things :)

You should either make the call
CreateMutex(NULL,TRUE,"CSLSharedMemLock");

or skip the <if> section there.

But sweep123 doesn't seem to want to own the mutex anyway, but take ownership only at a certain time.

@sweep123: what do you mean by:

<<But one of the programs is a DLL and that does not call
CloseHandle (m_hMutex);>> ?
Where is the m_hMutex variable defined?

~~Razvan

[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top