Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I dispose a Mutex?

I'm working on 2 Windows Services that have a common database which I want to lock (cross-process) with a system Mutex.

Now I'm wondering whether it's ok to just call WaitOne() and ReleaseMutex() in a try-finally block or should I also dispose the Mutex (e.g. in a using block). If so I guess I should always catch the AbandonedMutexException on the WaitOne() method or am I wrong here?

like image 697
Koen Avatar asked Aug 18 '11 12:08

Koen


People also ask

Does disposing a mutex Release it?

Since the proper pattern is to release a mutex before disposing it, the fact that code disposes a mutex without releasing it implies that something went wrong somewhere.

Can you reuse a mutex?

A mutex must be created once. Calling the pthread_mutex_init subroutine more than once with the same mutex parameter (for example, in two threads concurrently executing the same code) should be avoided. The second call will fail, returning an EBUSY error code.

What happens if mutex is not released?

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.

How do you release a mutex?

A mutex can be released only by the thread that acquired it. By contrast, the Semaphore class does not enforce thread identity. A mutex can also be passed across application domain boundaries and used for interprocess synchronization.


3 Answers

A mutex is a Windows kernel object (here wrapped in a .NET object).

As such, it is an unmanaged resource that should be disposed.

More accurately, the .NET object contains a HANDLE to the mutex, which must be released/disposed of somehow.

I don't trust that code sample in the Mutex class docs where the mutex object is not disposed. Although Henzi has a good point in comment: The Mutex object is static and would be either disposed by the finalizer or destroyed by the Windows kernel when the process exits.

Also, note that Close() disposes the object as well.

Of course, there's nothing wrong with keeping an existing Mutex object in your app even while you don't use it. They are light resources.

like image 117
Serge Wautier Avatar answered Oct 19 '22 21:10

Serge Wautier


According to this a named Mutex is automatically destroyed when the last process holding a HANDLE of that Mutex ends.

In non-managed terms MSDN says

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.

In .NET you should call .Close() on the Mutex - this releases the HANDLE... since every process gets its own HANDLE when accessing even the same named Mutex this is consistent practice... not calling Close() won't leave any problems behing once the process is no more (finalizers and all)...

like image 11
Yahia Avatar answered Oct 19 '22 19:10

Yahia


You need to dispose the resources which are used by the waithandle.

From the documentation:

Releases all resources used by the current instance of the WaitHandle class. (Inherited from WaitHandle.)

The waithandle uses unmanaged resources which should be disposed at the end of use.

MSDN Documentation Mutex

like image 2
Peter Avatar answered Oct 19 '22 20:10

Peter