Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

c# - How to gracefully get out of AbandonedMutexException?

I use the following code to synchronize mutually exclusive access to a shared resource between several running processes.

The mutex is created as such:

Mutex mtx = new Mutex(false, "MyNamedMutexName");

Then I use this method to enter mutually exclusive section:

public bool enterMutuallyExclusiveSection()
{
    //RETURN: 'true' if entered OK, 
    //         can continue with mutually exclusive section
    bool bRes;

    try
    {
        bRes = mtx.WaitOne();
    }
    catch (AbandonedMutexException)
    {
        //Abandoned mutex, how to handle it?

        //bRes = ?
    }
    catch
    {
        //Some other error
        bRes = false;
    }

    return bRes;
}

and this code to leave it:

public bool leaveMutuallyExclusiveSection()
{
    //RETURN: = 'true' if no error
    bool bRes = true;

    try
    {
        mtx.ReleaseMutex();
    }
    catch
    {
        //Failed
        bRes = false;
    }

    return bRes;
}

But what happens is that if one of the running processes crashes, or if it is terminated from a Task Manager, the mutex may return AbandonedMutexException exception. So my question is, what is the graceful way to get out of it?

This seems to work fine:

    catch (AbandonedMutexException)
    {
        //Abandoned mutex
        mtx.ReleaseMutex();
        bRes = mtx.WaitOne();    
    }

But can I enter the mutually exclusive section in that case?

Can someone clarify?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to MSDN the AbandonedMutexException is:

The exception that is thrown when one thread acquires a Mutex object that another thread has abandoned by exiting without releasing it.

This means that the thread in which this exception was thrown is the new owner of the Mutex (otherwise calling the Mutex.ReleaseMutex Method like you're doing would trigger an ApplicationException), and if you can assure the integrity of the data structures protected by the mutex you can simply ignore the exception and continue executing your application normally.

However, most of the times the AbandonedMutexException is raised the integrity of the data structures protected by the mutex cannot be guaranteed, and that's why this exception was introduced in the version 2.0 of the .NET framework:

An abandoned mutex indicates a serious programming error. When a thread exits without releasing the mutex, the data structures protected by the mutex might not be in a consistent state. Prior to version 2.0 of the .NET Framework, such problems were hard to discover because no exception was thrown if a wait completed as the result of an abandoned mutex.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...