Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abandoned mutex exception

I am trying to use a mutex for the first time and have the following code executing on two separate instances of the program

public void asynchronousCode()
    {
        using (var mutex = new Mutex(false, "mySpecialMutex"))
        {
            if (!mutex.WaitOne(1000, false))
            {
                Console.WriteLine("First check - some one locked the mutex");
            }

            if (!mutex.WaitOne(3000, false))
            {
                Console.WriteLine("Second check- some one locked the mutex");
            }
            else
            {
                Console.WriteLine("I got the mutex");
                Console.WriteLine("sleeping");
                Thread.Sleep(3000);
                Console.WriteLine("Awaking and Releasing mutex");
                mutex.ReleaseMutex();
            }
        }
    }

When I run this, one of the instances (the one i run first) prints

I got the mutex
sleeping
awaking and releasing mutex

The other instance prints

First check - some one locked the mutex

and as soon as the first instance leases the mutex, it crashes at the second wait statement with the exception

The wait completed due to an abandoned mutex.

Any ideas on why I am getting this exception and how i can prevent it ?

Solution: I probably should have read the mdsn documentation more clearly. Thanks Andrew for pointing me in the right direction

You can use the WaitHandle.WaitOne method to request ownership of a mutex. The thread that owns a mutex can request the same mutex in repeated calls to WaitOne without blocking its execution. However, the thread must call the ReleaseMutex method the same number of times to release ownership of the mutex. The Mutex class enforces thread identity, so a mutex can be released only by the thread that acquired it.

like image 995
nighthawk457 Avatar asked May 10 '12 17:05

nighthawk457


People also ask

What is an abandoned mutex?

An abandoned mutex indicates a serious programming error.

How do you release a mutex?

Use the MutexRelease function to release control of a mutex, unblocking one thread waiting to acquire the mutex.

What is mutex in C# with example?

A mutual exclusion (“Mutex”) is a mechanism that acts as a flag to prevent two threads from performing one or more actions simultaneously. A Mutex is like a C# lock, but it can work across multiple processes. In other words, Mutex can be computer-wide as well as application-wide.


1 Answers

Your problem is you hold the Mutex twice, but only release it once, due to how you have mis-arranged your if statements. Your first execution catches it twice - in both of those if statements, yet your code only releases it once.

You need to re-organize the ifs so you only capture the mutex one time.

bool captured = true;
if (!mutex.WaitOne(1000, false))
{
        Console.WriteLine("First check - some one locked the mutex");
        captured = false;
}
if (!captured && !mutex.WaitOne(3000, false))
{
        Console.WriteLine("Second check- some one locked the mutex");
        captured = false;
}
if (captured)
{
        Console.WriteLine("I got the mutex");
        Console.WriteLine("sleeping");
        Thread.Sleep(3000);
        Console.WriteLine("Awaking and Releasing mutex");
        mutex.ReleaseMutex();
}
like image 194
Andrew Barber Avatar answered Sep 24 '22 19:09

Andrew Barber