I have an application code in which I use a mutex to synchronise some code during the creation of an object. The object constructor acquires the mutex and ONLY releases it when the object is no longer needed thus one place to release the mutex would be in the object destructor. As I debugged the code using 2 instances of the app, the 1st instance first acquires the mutex, the 2nd instance sits and waits (mut.WaitOne()). User then closes the 1st app instance. At this instance, the 2nd instance mut.WaitOne() throws the exception: "The wait completed due to an abandoned mutex." This happens before the mut.ReleaseMutex() was called in the 1st instance (I know it because it hit my breakpoint in the object destructor code before calling MutexRelease). It appears that the mutex was released before ReleaseMutex() was called thus causing the exception. How would I resolve this race condition? Thank you for your help.
public sealed class MyObject
{
static ExtDeviceDriver devDrv;
private Mutex mut = new Mutex(false,myMutex);
public MyObject()
{
mut.WaitOne();
//Thread safe code here.
devDrv = new ExtDeviceDriver();
}
~MyObject()
{
mut.ReleaseMutex();
}
}
Destructors are not deterministic; there is no guarantee by the CLR as to when they will run. Instead, implement IDisposable
in your class, and force callers to use its instances in using(...)
blocks. That will ensure that your implementation of Dispose
gets called when it needs to.
E.g.,
public sealed class MyObject : IDisposable
{
static ExtDeviceDriver devDrv;
private Mutex mut = new Mutex(false,myMutex);
public MyObject()
{
mut.WaitOne();
//Thread safe code here.
devDrv = new ExtDeviceDriver();
}
public void Dispose() {
mut.ReleaseMutex();
}
}
Then callers would just need to do this:
using (var x = new MyObject()) {
// etc
}
When the execution flow exists the using
block, Dispose
will be called, regardless of exceptions or anything else.