Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i know how to use mutex in a single process..
i want to know how to use the mutex across the process in c

can u help me. please..
Posted
Comments
harish85 29-Jun-11 2:17am    
can you explain bit more your problem, Thanks

When you call CreateMutex[^] function in both processes, specify the same name (last parameter, lpName). This function first tries to find a mutex with specified name, and if such mutex exists, returns its handle; otherwise, it creates new mutex. So, the first process to call CreateMutex(, , "MyMutex") shall try to find "MyMutex", fail to do that and then create mutex "MyMutex". Call from the second process discovers existing object and returns its handle (you can check that mutex is opened, not newly created, by calling GetLastError() function, it should return ERROR_ALREADY_EXISTS code).
One more note: when you select mutex name, decide whether you want this mutex to be accessible across user sessions or not. When more than one user is logged in on the same computer simultaneously, kernel object's names from one user session won't be visible in another session unless they start with "Global\" prefix (see more about kernel object namespaces here[^])
 
Share this answer
 
v2
Comments
@BangIndia 29-Jun-11 2:32am    
Thanks.................................
You can't use a mutex in a single process - the whole idea is that they work across multiple processes.
Mutexes (mutual-exclusion locks) are used to implement critical sections and protect shared mutable data structures against concurrent accesses.
By definition, a single process cannot use a mutex, because it has no other accesses to worry about...
 
Share this answer
 
Comments
Timberbird 29-Jun-11 2:40am    
Sorry, why "can't"? As far as I know, it's entirely possible to use mutex to synchronyze threads in one process, just not recommended - critical sections are way faster.

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