ReleaseMutex

The ReleaseMutex function releases ownership of a mutex object. A mutex object can only be released by the thread that owns it.

Syntax

BOOL ReleaseMutex(
  HANDLE  hMutex
);
            

Parameters

Parameter Description
hMutex A handle to the mutex object to be released. This handle must have been created by the CreateMutex or OpenMutex function.

Return Value

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

Remarks

A thread uses the ReleaseMutex function to release its ownership of a mutex object. Each call to ReleaseMutex by the calling thread reduces the mutex's count by one.

The mutex object remains in the signaled state until its count reaches zero. When the mutex's count reaches zero, the mutex becomes owned by the calling thread, and the function returns nonzero. If the calling thread does not own the mutex object, the function returns zero and GetLastError returns ERROR_NOT_OWNER.

A thread can own a mutex object that has been created as a recursive mutex. This means the thread can call ReleaseMutex multiple times without blocking. For a nonrecursive mutex, the thread must call ReleaseMutex only once for each time it successfully called WaitForSingleObject or WaitForMultipleObjects to acquire the mutex. If a thread calls ReleaseMutex more times than it has called WaitForSingleObject or WaitForMultipleObjects to acquire the mutex, the mutex count is incremented beyond zero, and the calling thread remains the owner. The subsequent calls to WaitForSingleObject by other threads will block until the owner thread calls ReleaseMutex enough times to bring the count to zero.

Note

If the thread that owns a mutex object terminates without releasing it, the mutex object is considered abandoned. If another thread attempts to wait for the abandoned mutex, the wait function returns with a status code of WAIT_ABANDONED.

Important

Releasing a mutex that you do not own can lead to unpredictable behavior and deadlocks. Always ensure you are releasing a mutex that your thread currently owns.

Requirements

Component Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header synchapi.h
Library Kernel32.lib
DLL Kernel32.dll

See Also