Can someone explain this remark in the MSDN documentation of CreateMutex () on the bInitialOwner flag?

The MSDN documentation for CreatMutex () ( http://msdn.microsoft.com/en-us/library/ms682411%28VS.85%29.aspx ) contains the following note near the end:

Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes with sufficient access rights simply open the handle to the existing mutex. This allows multiple processes to receive descriptors of the same mutex, freeing the user from the obligation to ensure that the creation process is launched first. When using this method, you must set the bInitialOwner flag to FALSE; Otherwise, it can be difficult to verify which process has its original ownership.

Can someone explain the problem with bInitialOwner = TRUE?

Earlier in the same documentation, he offers a call to GetLastError () that will allow you to determine whether the call to CreateMutex () called the mutex or simply returned a new handle to the existing mutex:

Return value

If the function succeeds, the return value is a handle to the newly created mutex object.

If the function does not work, the return value is NULL. To get extended error information, call GetLastError.

If the mutex is a named mutex, and the object existed before calling this function, the return value is a handle to an existing object, GetLastError returns ERROR_ALREADY_EXISTS, bInitialOwner is ignored, and ownership is not granted to the calling thread. However, if the caller has limited access rights, the function will fail with ERROR_ACCESS_DENIED, and the caller must use the OpenMutex function.

+4
winapi
Jun 03 '10 at 14:01
source share
3 answers

Using bInitialOwner combines two steps into one: creating a mutex and getting a mutex. If several people can create mutexes at the same time, the first step may fail, while the second step can be successful.

As the other respondents noted, this is not strictly a problem, since you will get ERROR_ALREADY_EXISTS if someone else creates it first. But then you must distinguish between the cases “could not create or find mutexes” and “could not get mutexes, try again later”, simply using the error code. This will make your code hard to read and easier to corrupt.

In contrast, when bInitialOwner is FALSE, the stream is much simpler:

 result = create mutex() if result == error: // die result = try to acquire mutex() if result == error: // try again later else: // it worked! 
+3
Jun 07 '10 at 5:20
source share

Well, not sure if there is a real problem. But if you set the argument to TRUE in both processes, then you should check the value of GetLastError () to see if you really got the ownership. This will be the first serve. Useful, perhaps only if you use a named mutex to implement an instance of a singleton process.

+1
Jun 03 2018-10-06
source share

The flag is used to create a mutex in its own state - a successful caller will atomize the creation of a synchronization object, and also get a lock before returning in case the caller must be sure that no race condition can be formed between the creation of the object and its acquisition.

Your protocol will determine if you need to do this in one atomic operation.

0
Jun 06 '10 at 21:39
source share



All Articles