I have written a sample console application which creates a mutex as shown in below code sample. I am launching this application directly from Visual Studio (VS2013) by pressing Ctrl + F5 (running the application without debugger). For the 1st instance of console application I acquire the mutex and the following line is displayed in console:
New Instance created...
However, when I create a second instance of console application using Ctrl + F5 again, I get the following message in the console:
Instance already acquired...
even though I explicitly release mutex after 500ms with this line of code:
mut.ReleaseMutex();
In the same thread which acquires the mutex, I still see that my second instance of console application waits for the mutex to be released.
Can someone explain to me why is it so or correct me if I am doing something wrong? If I understand, ReleaseMutex
should release the mutex from the same thread which acquires it via mut.WaitOne(0)
call so that any other thread waiting to acquire mutex should be provided ownership. However, in this case I am not able to see it working.
If I close the 1st instance which acquired mutex (still my second is alive) and try to launch a 3rd instance with Ctrl+F5, I can see that there is an AbandonedMutexException
:
Unhandled Exception: System.Threading.AbandonedMutexException: The wait completed due to an abandoned mutex.
PS: Interestingly I can see that this works fine if I pass false
in mutex constructor as
static Mutex mut = new Mutex(false, "Global\\test");
What's the significance of initiallyOwned
parameter in the
public Mutex(bool initiallyOwned, string name);
constructor version of mutex class?
Console output for 2 instances run from VS
class Program
{
static Mutex mut = new Mutex(true, "Global\\test");
static void Main(string[] args)
{
if (IsInstance())
{
Console.WriteLine("New Instance created...");
}
else
{
Console.WriteLine("Instance already acquired...");
}
Console.ReadLine();
}
static bool IsInstance()
{
if (!mut.WaitOne(0))
{
Console.WriteLine("Thread id {0} Waiting at Mutex...",AppDomain.GetCurrentThreadId());
return false;
}
else
{
Console.WriteLine("Thread id {0} got Mutex...", AppDomain.GetCurrentThreadId());
Thread.Sleep(500);
mut.ReleaseMutex();
return true;
}
}
}
true
asinitiallyOwned
, the current thread will immediately try to acquire the mutex after it is created. – Leandromut.ReleaseMutex();
on the same thread is not releasing the mutex ?? This is what I am trying to understand. It is interesting to see withfalse
it works fine, why not withtrue
. If it is intended to work so , in which situations shouldtrue
be used and whichfalse
. – AlbyWaitOne
check = no release = abandoned mutex.WaitOne
is not "check if this thread has ownership", it's "try to take ownership", so it will returnfalse
(because something already own mutex). Read more about abandoned mutex. – Sinatr