Home > Software engineering >  About the CreateMutex return values
About the CreateMutex return values

Time:10-04

 
HANDLE hMutexA=CreateMutex (NULL, false, "MyAppMutex");
If (hMutexA==NULL)
{
Cout & lt; <"Fail" & lt; }
The else
{
Cout & lt; DWORD dwLastError=GetLastError ();
If (dwLastError==ERROR_ALREADY_EXISTS)
Cout & lt; <"Aleady exists" & lt; The else
Cout & lt; <"New one" & lt; }


The code above I in vs2010debug mode not debugging run time each time the returned handle values are the same, but if I debug runtime and open handle value is different, this is what reason, please explain a great god
If at first you don't debugging running

The second time debugging and running
don't
Debugging and running
the third time

CodePudding user response:

CreateMutex
The CreateMutex function creates a named or unnamed mutex object.

HANDLE CreateMutex (
LPSECURITY_ATTRIBUTES lpMutexAttributes,
//pointer to the security attributes
BOOL bInitialOwner,//flag for initial ownership
LPCTSTR lpName//pointer to the mutex object name
-);

The Parameters
LpMutexAttributes
Pointer to a SECURITY_ATTRIBUTES structure that determines been the returned handle can be inherited by child the processes. If lpMutexAttributes is NULL, the handle always be inherited.
Windows NT: The lpSecurityDescriptor member of The structure specifies a security descriptor for The new mutex. If lpMutexAttributes is NULL, The mutex gets a default security descriptor.

BInitialOwner
Specifies the initial owner of the mutex object. If this value is TRUE and the caller created the mutex, the calling thread obtains ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex) To determine If the caller created the mutex, see the Return Values section.
LpName
Pointer to a null - terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters and can contain any character except the backslash path - the separator character (\). The name comparison is case sensitive.
If lpName matches the name of an existing named mutex object, this function requests MUTEX_ALL_ACCESS access to the existing object. In this case, the bInitialOwner parameter is ignored because it has already had been set by the creating process. If the lpMutexAttributes parameter is not NULL, it determines been the handle can be inherited, but its security descriptor - member is ignored.

If lpName is NULL, the mutex object is created without a name.

If lpName matches the name of an existing event, semaphore, waitable timer, job, or file - mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.

The Return Values
If the function succeeds, the return value is a handle to the mutex object. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex.

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

Few
The handle returned by CreateMutex has MUTEX_ALL_ACCESS access to The new mutex object and can be 2 in any function that requires a handle to a mutex object.

Any thread of the calling process can specify the mutex object - handle in a call to one of the wait functions provides. The single - object wait functions provides the return when the state of the specified object is signaled. The multiple - object wait functions provides can be instructed to return either when Any one or the when all of the specified objects are signaled. When a wait function returns, the waiting thread is released to continue its execution.

The state of a mutex object is signaled when it is not owned by any thread. The creating thread can use the bInitialOwner flag to request immediate ownership of the mutex. Otherwise, a thread must use one of the wait functions to request ownership. When the mutex's state is signaled, one waiting thread is granted ownership, the mutex's state changes to nonsignaled, and the wait function returns. Only one thread can own a mutex at any given time. The owning thread uses the ReleaseMutex function to release its ownership.

The thread that owns a mutex can specify The same mutex in repeated wait function calls without blocking its execution. Typically, you order The not wait repeatedly for The same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. Or, to release its ownership, The thread must call ReleaseMutex once for each time that The mutex satisfied a wait.

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related