Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception when releasing a Mutex. What could cause this?

I'm getting the following exception "Object synchronization method was called from an unsynchronized block of code" when releasing the mutex in the code below:

int count = 0;
try
{
     mutex.WaitOne();
     count = requests_sent.Count;
}
catch
{
}
finally
{
     mutex.ReleaseMutex();
}

requests_sent is a dictionary that is being accessed (read/write) by other threads but I have no clue why this code is throwing an exception. Anyone have any ideas?

like image 944
HasaniH Avatar asked Oct 06 '11 19:10

HasaniH


1 Answers

Move the WaitOne() call above the try block. You only want to release the mutex when you know you acquired it.

You'll now also stand a chance to get a better diagnostic.

like image 72
Hans Passant Avatar answered Oct 27 '22 05:10

Hans Passant