Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloseHandle on a Mutex, before ReleaseMutex - What happens?

If I call CloseHandle on a mutex before a thread has finished with the mutex, and hence, hasn't yet called ReleaseMutex, what is the expected behaviour?

like image 367
Richard Anthony Freeman-Hein Avatar asked Feb 17 '11 20:02

Richard Anthony Freeman-Hein


1 Answers

CloseHandle() immediately destroys the handle that is passed to it. ReleaseMutex() will then fail with an ERROR_INVALID_HANDLE error code if called with the closed mutex handle.

If the mutex is named, there is a single reference-counted kernel object backing the mutex, but CreateMutex() and OpenMutex() return unique HANDLE values that have to be closed individually. If multiple handles to the same named mutex are created/opened, calling CloseHandle() on one handle does not effect the other handles to the same mutex.

like image 175
Remy Lebeau Avatar answered Oct 02 '22 09:10

Remy Lebeau