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

  • SEARCH
  • Home
  • 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 7918493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:31:52+00:00 2026-06-03T15:31:52+00:00

I am trying to use a mutex for the first time and have the

  • 0

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.

  • 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-03T15:31:54+00:00Added an answer on June 3, 2026 at 3:31 pm

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Hi I'm trying use a datepicker on a field I have. I'm trying to
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
I am trying to use boost::shared_mutex to implement a multiple-reader / single-writer mutex. My
I'm trying to understand how to use mutexes with objects in c++. I have
I have a regex that I'm trying use to validate against strings. Trying to
I have a 3rd party DLL that I am trying to use in a
I'm trying to use the mutex method for only allowing one instance of my
We have code that makes use of sched_yield inside a loop. When we do
I'm trying to use a mutex to protect access to some hardware from multiple

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.