Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Mutex –in windows- seems not to be a safe way to solve concurrency access.
Can anybody confirm this?

A mutex should be able to control access to any resources. In my case I use a named mutex, but windows seems not to be able to control this beyond sessions…even I use prefix “Globals\” for mutex name.

Problem:
A process will be started by a windows service. This process does create a mutex “Global\xyz”.
A second process started by myself by double clicking the exe in the explorer. This process also creates the mutex “Global\xyz”...successfully....what I do not expect...

Same behaviour in xp and W7.

Where I’m wrong? Any suggestions? Is it a windows bug?
Posted
Comments
CPallini 19-Jan-14 11:45am    
Silly question: have you properly escaped the backslash in the mutex name?
[no name] 19-Jan-14 11:55am    
Sorry I probably missunderstood your comment....very sorry!
CPallini 19-Jan-14 12:13pm    
No problem.
hence you should write "Global\\xyz" and test it again.
[no name] 19-Jan-14 13:01pm    
Of course in my c++ code I wrote "Global\\xyz", otherwhise it will not compile ;).
To be precise: My mutex name is the application exe name (Param(0)) from where I remove all "\" and preceed it with "Global\". You can trust, that I'm safe in these basics.
CPallini 19-Jan-14 13:15pm    
I would compile. I would just issue a warning.
Of course I trust you :-)

1 solution

check the error code with GetLastError() -> it should be ERROR_ALREADY_EXISTS
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682411(v=vs.85).aspx[^]
 
Share this answer
 
Comments
[no name] 19-Jan-14 13:30pm    
Thanks for this. Allready checked with GetLastError. For xp and W7 same unexpected behaviour, when first process has been started by a windows service.
[Edit]widows->Windows[Edit] greetings from "freud": http://en.wikipedia.org/wiki/Sigmund_Freud :)
SoMad 19-Jan-14 22:56pm    
Can you clarify this? In my experience, KarstenK is correct. The return value is a handle to the existing mutex and GetLastError() returns ERROR_ALREADY_EXISTS just as documented[^]. It is then up to you to make you program behave 'correctly', whatever that is in the specific context of your application.

Soren Madsen
[no name] 20-Jan-14 3:31am    
I agree, it should work in the way KarstenK described it. And this is also the case when I start the application by myself from the explorer. But as soon I start the first application from a service and the second from the explorer, both can create and own the mutex. Means vor both GetLastError return 0.
SoMad 20-Jan-14 12:03pm    
That does sound weird. I also have projects where Windows services and applications access a mutex and I do not have the problem you are describing. In most of our cases (possibly all our cases), we are creating the mutex and applying a Security Descriptor in order to limit access to the mutex based on the logged in user's privileges, but I don't know if that would change the behavior.

Soren Madsen
[no name] 20-Jan-14 12:35pm    
It is really a confusing thing:
- On XP one has to activate FUS (fast user switching) which is not available if the pc is in a domain. So here I made no further tests.
- On W7: Very surprising!!
My mutex name in real is not simply "Global\xyz". I create it from Param(0) (the exe path) and remove from Param(0) all backslashes. After a lot of trials I was near to give up and in my despair I made a last test, where I also removed ":" from the Param(0)....and.... I can not believe it now it works.
BTW: GetLastError does return ACCESS_DENIED for the second started process and not ERROR_ALREADY_EXISTS
Thank you for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900