简体   繁体   中英

what happens to a mutex object if not released by parent applicaiton

I have a named mutex in my application.

 private Mutex TaskExitStatus = new Mutex(false, "TaskExit")

I acquire the mutex in a method using TaskExitStatus.WaitOne()

What will happen if I am not releasing the mutex while exiting the application? Will the mutex be disposed safely?

Update 1: Will both named and unnamed mutex behave the same when not released on application exit?

Update 2 This is what i read from MSDN link

Use the CloseHandle function 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.

According to the documentation , it is abandoned (not necessarily disposed)

If a thread terminates while owning a mutex, the mutex is said to be abandoned

Also, heed the warning about abandoned Mutexes:

An abandoned mutex often indicates a serious error in the code. When a thread exits without releasing the mutex, the data structures protected by the mutex might not be in a consistent state. The next thread to request ownership of the mutex can handle this exception and proceed, if the integrity of the data structures can be verified.

The purpose of a Mutex is to maintain a lock (for named Mutexes across applications) until it is released. If you do not release the Mutex the lock should be maintained on the resource until the computer is rebooted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM