RE: Mutex Problem
- From: lelteto <lelteto@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 21 Jul 2007 22:50:02 -0700
1. IF you know that one application is the master (and it most work before
the slave can) then yes, you can use CreateMutex in the master and OpenMutex
in the slave. Note, however, that if the slave app starts earlier, then it
would not get the mutex. (Since nothing to process anyway, one option is to
periodically re-try opening the mutex.)
2. Yes, good programming practice requires that each and every application
releases all resources it acquires. And that includes closing ALL handles the
3. On the other hand, Windows should automatically close all handles anyway
when an app terminates. (But are you sure your app fully terminates and the
slave app doesn't sometimes has another - maybe non-functional, but not
temrinated thread running?) You can check wth Task Manager if the slave app
4. Note, that Windows may delay closing handles if it thinks it is faster to
keep a resorce open because maybe the same app will use it again. (Although I
don't know how this interferes with the app-termination cleanup part of the
I have 2 applications that I need to synchronize their mutual access of some
shared memory, and for this reason each of these applications tries to create
an unowned named-mutex upon its startup:
hMutex = CreateMutex (NULL, false, szMutexName);
From this moment and on, any of them gets hold of the mutex before accessing
the shared memory, and releases it immediately afterwards:
if (WAIT_OBJECT_0 != WaitForSingleObject (hMutex, 1000))
<access the shared memory>...
NOTE: NONE of the applications call CloseHandle (hMutex)!
To be more accurate, one of them acts as a master (that is, makes both
read/write operations), and the other as a slave (that is, may only read the
Now, everything works fine all along, no matter what's the first application
to run (and create the mutex), and what's the second.
BUT when the slave ends running and closes, the master application only
partially succeeds getting hold of the mutex (that is, sometimes it get hold
of the mutex, sometimes not, in a seemingly repeating way), and therefore
cannot properly funtion.
1) Why is it so, and how can I solve it?
2) Should the slave application close the mutex somehow upon closing?
3) Should I use OpnetMutex() instead of CreateMutex()?
Thanks for your help,
- Re: Mutex Problem
- From: Skywing [MVP]
- Re: Mutex Problem
- Prev by Date: Re: Error in installing certificate Using certenroll
- Next by Date: GINA
- Previous by thread: Error in installing certificate Using certenroll
- Next by thread: Re: Mutex Problem