Discussion:
WaitForSingleObject fails even after mutex is released
(too old to reply)
g***@hotmail.com
2005-11-16 18:08:41 UTC
Permalink
I have a global variable sMutexName which stores the name of the mutex
like you did.


The 1st instance creates the mutex.
CreateMutex(sa, 0&, sMutexName) 'Owner flag is 0, so it will not own
the mutex
where sa is SECURITY_ATTRIBUTES
with sa.nLength = Len(sa)
sMutexName = lpName ' lpName is the name for the mutex


The 2nd instance opens the mutex and waits to grab the mutex.
OpenMutex(SYNCHRONIZE, 0&, sMutexName)
WaitForSingleObject(sMutexName, lTimeOut) 'lTimeOut is currently set to

100


Then it releases the mutex with
ReleaseMutex(sMutexName)
If bInitialOwner Then
Call CloseHandle(sMutexName)
End If
But the form is not unloaded (it stays).


3rd instance is trying to do the same thing as the 2nd one AFTER the
2nd releases mutex. The 3rd instance keeps getting timeout at the
WaitForSingleObject call. Now what's causing the 3rd instance getting
timeout from
WaitForSingleObject call? It succeeds in OpenMutex. I am
confused. Anyone can explain this scenario? Thanks.
Kellie Fitton
2005-11-16 22:24:48 UTC
Permalink
Hi,

The API WaitForSingleObject() requires a HANDLE for its first
parameter that was returned by the API's CreateMutex() or
OpenMutex(), likeWise, the API's ReleaseMutex() & CloseHandle()
requires the same type of handle as well. ??

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitforsingleobject.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createmutex.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/openmutex.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/releasemutex.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/closehandle.asp

Kellie.
g***@hotmail.com
2005-11-17 14:58:43 UTC
Permalink
Sorry, I changed to use the handle returned by CreateMutex or OpenMutex
to do the ReleaseMutex and WaitForSingleObject, but it still did not
work.
1st instance:
IMutexID = CreateMutex(sa, 0&, sMutexName)

2nd instance:
IMutexID = OpenMutex(SYNCHRONIZE, 0&, sMutexName)
WaitForSingleObject(IMutexID , lTimeOut)

Then it releases the mutex:
Release = ReleaseMutex(lMutexID)
If bInitialOwner Then
Call CloseHandle(lMutexID)
End If

but leaves the form intact.

3rd instance:
IMutexID = OpenMutex(SYNCHRONIZE, 0&, sMutexName)
WaitForSingleObject(IMutexID , lTimeOut)
But the WaitForSingleObject times out. Why? The mutex has been
released by the 2nd the instance. Even though the 1st instance stays
on, it does not own the mutex. Could anyone please explain this
scenario? Thanks.
Kellie Fitton
2005-11-18 03:46:10 UTC
Permalink
Hi,

Well, since you are creating the mutex correctly with the API
CreateMutex() which returns the needed handle, then you don't
need to use the function OpenMutex(), and to Request ownerShip
of the mutex object you can use the API WaitForSingleObject(),
and finally you can Release the ownerShip of the mutex object
by calling the API ReleaseMutex(), someThing like this:

CreateMutex(NULL, FALSE, NULL);
WaitForSingleObject(hMutex, 5000L);
//* do someThing with the process... *//
ReleaseMutex(hMutex);

Also, after you create the mutex, use the following API's to make
sure that the function call did not fail:

GetLastError()
FormatMessage()

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getlasterror.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp

Hope these information helps,

Kellie.
g***@hotmail.com
2005-11-18 16:02:33 UTC
Permalink
Kellie,

Thanks so much for the information. But I still could not understand
why the 3rd instance of the executable cannot grab the mutex with
WaitForSingleObject while the 2nd one had already released the mutex.
(The 1st one still running, but it does not own the mutex).

The reason that I used OpenMutex in the 2nd instance was to get the
handle. The 1st instance of my executable creates the mutex and stays
running. The 2nd one gets the handle from OpenMutex and obtains
ownership from WaitForSingleObject and it succeeds everytime. The 2nd
one then releases the mutex by calling ReleaseMutex with the handle.
However, even after the mutex is released, the 3rd comes in with
OpenMutex to get the handle and then WaitForSingleObject, but the WAIT
always times out. Why?
Sten Westerback (MVP SDK)
2005-11-18 16:45:08 UTC
Permalink
Post by g***@hotmail.com
Kellie,
Thanks so much for the information. But I still could not understand
why the 3rd instance of the executable cannot grab the mutex with
WaitForSingleObject while the 2nd one had already released the mutex.
(The 1st one still running, but it does not own the mutex).
The reason that I used OpenMutex in the 2nd instance was to get the
handle. The 1st instance of my executable creates the mutex and stays
running. The 2nd one gets the handle from OpenMutex and obtains
ownership from WaitForSingleObject and it succeeds everytime. The 2nd
one then releases the mutex by calling ReleaseMutex with the handle.
However, even after the mutex is released, the 3rd comes in with
OpenMutex to get the handle and then WaitForSingleObject, but the WAIT
always times out. Why?
First of all you haven't told us how many ms your third instance waits for
the
mutex nor when the applications are started.

It's also a bit unclear what you mean by "But the form is not unloaded (it
stays)."
in the second instance case. Do you mean the window (and the process) stays
active but you are 100% sure you don't rereserve the mutex later on?

A final comment is that if you aren't 100% sure of the process startup
sequence
then you should use CreateMutex() in all cases -- it is not recreated if it
already exists.

Also, i can't see the start of this news thread... or who is the "you" the
OP refer
to in message Nov 16th 20:08 ?

- Sten
g***@hotmail.com
2005-11-18 18:31:43 UTC
Permalink
Sten,
I originally posted this message in another group and I simply copied
over. Sorry for the confusion.
1) The timeout interval for WaitForSingleObject is currently set to
100ms
2) I start the 1st instance first which creates mutex without
ownership, but with a name. A handle is returned. 1st instance stays
on.
3) Once the 1st one is up, I start the 2nd instance with OpenMutex by
the same name the 1st one creates. It then WaitForSingleObject using
the handle returned by OpenMutex and timeout 100ms. It succeeds in
grabbing the ownership of the mutex. It releases the mutex using
ReleaseMutex with handle after the window comes up. The window stays
on. I do not find anywhere else that the mutex is regained. Besides,
WaitForSingleObject, how would you regain the mutex?
4) After the 2nd instance releases the mutex, I start the 3rd one which
opens the mutex using OpenMutex with the same name, then
WaitForSingleObject with the handle returned by OpenMutex. I thought
the 3rd one will obtain the ownership of the mutex, but it keeps
timeout at WaitForSingleObject. I just wanted to find out a reason.

Thanks.

Loading...