Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:29:23+00:00 2026-06-02T01:29:23+00:00

I don’t understand why the .NET mutex does not either throw an AbandonedMutexException in

  • 0

I don’t understand why the .NET mutex does not either throw an AbandonedMutexException in one of the waiting threads or release the mutex when Mutex.Dispose() is called.

In particular, code like this will deadlock:

public static void testCall()
{
    using (var mutex = new System.Threading.Mutex(false, "testname"))
    {
        mutex.WaitOne();
        Console.WriteLine("second call");
    }
}

public static void Main(string[] args)
{
    var thread = new System.Threading.Thread(testCall);
    using (var mutex = new System.Threading.Mutex(false, "testname"))
    {
        mutex.WaitOne();
        Console.WriteLine("first call");
        thread.Start();
        System.Threading.Thread.Sleep(new TimeSpan(0, 0, 5));
        Console.WriteLine("sleep done");
    }

    thread.Join();
}

Mind you, I understand the AbandonedMutexException usually comes from the underlying WIN32 mutex and in native code would only be triggered in case the owning thread dies – I’ve been writing C/C++ code for a long time and am fully aware of the underlying design. I also know that the following code is an easy workaround:

using (var mutex = new System.Threading.Mutex(false, "testname"))
{
    mutex.WaitOne();
    try
    {
        Console.WriteLine("first call");
        thread.Start();
        System.Threading.Thread.Sleep(new TimeSpan(0, 0, 1));
        Console.WriteLine("sleep done");
    }
    finally
    {
        mutex.ReleaseMutex();
    }
}

What I don’t understand is the rationale behind .NET mutexes not forcing a release when the object has been explicitly disposed of while holding the lock. Wouldn’t that be more in-line with the rest of the .NET programming paradigm? If/when the developer explicitly destroys a locked mutex… it only makes sense to mark it as abandoned.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:29:25+00:00Added an answer on June 2, 2026 at 1:29 am

    You probably don’t want it to work the way that you suggest. Let’s say you have this:

    using (var m = new Mutex(....))
    {
        m.WaitOne();
        // do some stuff here
        // that ends up throwing an exception
    }
    

    The exception is thrown while your thread holds the mutex. If the mutex were released as part of the dispose, then some other thread could obtain the mutex and begin partying on the data that you were updating. Except that now the data is in an unknown (probably inconsistent or corrupt) state.

    It’s best, of course, to handle the exception and clean things up, but absent that I’d rather the mutex remain held (or abandoned if the thread dies) so that the next thread that tries to acquire the mutex knows that something bad happened.

    In addition to the above, adding automatic release would require that the .NET wrapper keep track of which thread owns the mutex. And the Dispose method would have to check that value to determine if it should call ReleaseMutex. And it’s just not possible to keep track of that. The .NET program could pass the mutex handle to some unmanaged code, which could acquire or release the mutex without the wrapper’s knowledge.

    So, the answer is twofold: First, it’s not possible. Second, even if it were possible, you probably don’t want that behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't quite understand why this copy constructor is not invoked when I build with
Don't know the term for the red validation border, does it have one? I
Don't want to sort the entries. using this does not preserve the order as
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
Don't think that I'm mad, I understand how php works! That being said. I
Don't know why people do not practice AJAX implementation for authentication systems. Is it
I don't know if this question is trivial or not. But after a couple
Don't know a whole lot about streams. Why does the first version work using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.