Creates a named or unnamed mutex object.
HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOLEAN bInitialOwner, LPCTSTR lpName ); HANDLE RtCreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, BOOLEAN bInitialOwner, LPCTSTR lpName );
lpMutexAttributes
bInitialOwner
lpName
bInitialOwner
parameter because it was already set by the creation process.
The mutex object's state is signaled when no thread owns it. The creating thread can use the bInitialOwner flag to request immediate mutex ownership. Otherwise, a thread must use a wait function 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 ReleaseMutex 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, the thread 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 ReleaseMutex once for each time that the mutex satisfied a wait.
Two or more processes can call CreateMutex to 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 you of the responsibility of ensuring that the creating process is started first. When using this technique, set the bInitialOwner flag to FALSE; otherwise, it can be difficult to determine which process has initial ownership.
Multiple processes can have handles of the same mutex object, enabling use of the object for interprocess synchronization. To provide object sharing, a process can specify the name of a mutex object in a call to CreateMutex.
To close the handle, use CloseHandle. The system automatically closes the handle when the process terminates. When its last handle closes, the mutex object is destroyed.
RtCreateMutex can also be used from a Windows application to create an iwin32 mutex.
For extended error information, see GetLastError.
Versions | Defined in | Include | Link to |
---|---|---|---|
INtime 3.0 | intime/rt/include/iwin32.h intime/rt/include/winbase.h |
iwin32.h windows.h |
iwin32.lib |
Notes
CloseHandle, ReleaseMutex, iwin32 API, iwin32 Overview