Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mutex Problem

0 views
Skip to first unread message

עפר

unread,
Jul 21, 2007, 3:54:01 PM7/21/07
to
Hi All,

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))
{
return (-1);
}

<access the shared memory>...

ReleaseMutex (hMutex);

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
shared memory).

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,
Ofer.


Kalle Olavi Niemitalo

unread,
Jul 21, 2007, 4:57:43 PM7/21/07
to
עפר <@discussions.microsoft.com> writes:

> if (WAIT_OBJECT_0 != WaitForSingleObject (hMutex, 1000))
> {
> return (-1);
> }

You should really check for WAIT_ABANDONED too.
Otherwise a process can acquire the mutex without realizing it,
and then lock others out.

עפר

unread,
Jul 22, 2007, 4:36:02 AM7/22/07
to
Hi Kalle,

Thanks for your answer.

Instead, calling CloseHandle() before leaving solved the problem, though I
really can't fully understand that.

Sincerely,
Ofer.

Ben Voigt [C++ MVP]

unread,
Aug 20, 2007, 9:41:48 AM8/20/07
to

"???" <@discussions.microsoft.com> wrote in message
news:6FE778C6-1F36-4993...@microsoft.com...

You still need to check for WAIT_ABANDONED, in case the other app was
forcibly closed (TerminateProcess) while holding the mutex.

>
> Sincerely,
> Ofer.
>


0 new messages