ReleaseMutex
The ReleaseMutex function releases the ownership of a mutex object.
ReleaseMutex must own the mutex. If the calling thread does not own the mutex, the function fails.
Syntax
BOOL ReleaseMutex(
HANDLE hMutex
);
Parameters
| Parameter | Type | Description |
|---|---|---|
hMutex |
HANDLE |
A handle to the mutex object to be released. This handle must have been created by the CreateMutex function.
|
Return Value
GetLastError.
Remarks
A thread uses ReleaseMutex to release its ownership of a mutex object.
The mutex is then said to be in the signaled state.
Other threads can now attempt to acquire ownership of the mutex by calling the WaitForSingleObject function or one of the related functions.
If the calling thread does not own the mutex, GetLastError returns ERROR_NOT_OWNER.
If the handle specified by the hMutex parameter is not a valid mutex handle, GetLastError returns ERROR_INVALID_HANDLE.
A thread should only call ReleaseMutex once for each time it has successfully acquired ownership of the mutex. Releasing a mutex more times than it has been acquired can lead to undefined behavior.
Example
The following example demonstrates how to release a mutex after using it to protect a shared resource.
// Assume hMutex is a valid handle to a mutex previously created with CreateMutex.
// Assume the current thread currently owns hMutex.
if (ReleaseMutex(hMutex)) {
// Mutex successfully released.
// Other threads can now acquire ownership.
} else {
// Handle the error, for example, by checking GetLastError().
DWORD error = GetLastError();
// Handle error...
}