Important: |
---|
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
This function creates a named or unnamed mutex object.
HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes , BOOL bInitialOwner , LPCTSTR lpName );
Parameters
If lpNamematches the name of an existing named mutex object, the bInitialOwnerparameter is ignored because it has already been set by the creating process.
If lpNameis NULL, the mutex object is created without a name.
If lpNamematches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastErrorfunction returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.
Return Values
A handle to the mutex object indicates success. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastErrorreturns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex. NULL indicates failure. To get extended error information, call GetLastError.
Remarks
The handle returned by CreateMutexhas MUTEX_ALL_ACCESS access to the new mutex object and can be used 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. The single-object wait functions return when the state of the specified object is signaled. The multiple-object wait functions can be instructed to return either when any one or 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 bInitialOwnerflag 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 ReleaseMutexfunction 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 would 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. However, to release its ownership, the thread must call ReleaseMutexonce for each time that the mutex satisfied a wait.
Two or more processes can call CreateMutexto create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwnerflag to FALSE; otherwise, it can be difficult to be certain which process has initial ownership.
Multiple processes can have handles of the same mutex object, enabling use of the object for interprocess synchronization. The following object-sharing mechanism is available: A process can specify the name of a mutex object in a call to the CreateMutexfunction.
Use the CloseHandlefunction to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
Requirements
Runs on | Versions | Defined in | Include | Link to |
---|---|---|---|---|
Windows CE OS | 1.01 and later | Winbase.h | Coredll.lib, Nk.lib |
Note This API is part of the complete Windows CE OS package as provided by Microsoft. The functionality of a particular platform is determined by the original equipment manufacturer (OEM) and some devices may not support this API.
See Also
CloseHandle, CreateProcess, ReleaseMutex