10
votes

What is wrong with this code? i get a 'Object synchronization method was called from an unsynchronized block of code'. I found one result on google that said i may be releasing a mutex before locking but according to my output this is not the case. Here is the mutex code without the other code in between.

-edit- sorry guys, wrong paste.

My output

1W
1W
2W

code

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace sqliteTest
{
    class Program
    {
        static volatile Mutex mut1 = new Mutex();
        static volatile Mutex mut2 = new Mutex();
        static void Main(string[] args)
        {
            mut1.WaitOne(); Console.WriteLine("1W");
            Thread oThread = new Thread(new ThreadStart(fn2));
            oThread.Start();
            mut1.WaitOne(); Console.WriteLine("1W");
            update(0);
        }
        static void fn2()
        {
            mut2.WaitOne(); Console.WriteLine("2W");
            mut1.ReleaseMutex(); Console.WriteLine("1R");
            mut2.WaitOne(); Console.WriteLine("2W");
            update(1);
            mut1.ReleaseMutex(); Console.WriteLine("1R");
        }
        static void update(int t)
        {
            mut2.ReleaseMutex(); Console.WriteLine("2R");
            if (t == 0)
            {
                mut1.WaitOne();
                Console.WriteLine("1W");
            }
        }
    }
}
4
Either line 37 or 49. Those are where both threads are in my debugger.user34537
oops. +1 dtb. In the newly pasted code it happens at 23 and 30user34537

4 Answers

22
votes

It is not a great error message, Windows produces it. What it really means is that you are calling ReleaseMutex on a mutex that you don't own. You'll get past the first exception with

static volatile Mutex mut2 = new Mutex(true);

But then it will die inside the thread when it calls ReleaseMutex on mut1, which it doesn't own. Not sure what you're trying to do, the code doesn't make much sense to me.

5
votes

Yes the others are right: This code does not make any sense.

But as this page has a high ranking in Google (although it is not helpfull) I redirect all the searchers to this great site about Thread synchronization:

http://www.albahari.com/threading/part2.aspx

Elmü

4
votes

Your code seems to have a number of problems. The main thread starts a new thread, then calls update. In update it tries to unlock mutex2, but it hasn't locked mutex2 yet, so this fails with an exception.

Even if this error is fixed, the other thread is equally doomed to failure. It will try to release mutex1 before it has locked it.

What are you trying to do here? Are you confusing Mutex with AutoResetEvent?

Also I'm guessing that these two lines are a copy/paste error because they appear twice:

        mut2.WaitOne(); Console.WriteLine("2W");
        mut1.ReleaseMutex(); Console.WriteLine("1R");
0
votes

Just wanted to post another odd scenario I ran into as it might help others. I was getting this exception when opening up a project in Visual Studio 2010 C# express. Now my project does contain some threading, but I wouldn't expect it to crash Visual Studio on loading the code, which is exactly what was happening.

A note about my code: I am using MethodInvoker and BeginInvoke (after checking if InvokeRequired) called on a STA thread ([STAThreadAttribute]). Again, this code never gets a chance to even run. Visual Studio Express would crash just loading up the project. Also, this all started happening after I got a strange FileNotFoundException that was likewise crashing VS when it claimed it couldn't find a reference. I fixed that by eventually completely deleting the reference and all of its usage from the code, building, closing VS, opening it up again and opening the solution up, only to then begin hitting this mysterious error. No Mutexes or Monitors in this project or associated with that previously removed reference (a custom library that just draws some graphs).

After considerable research (googling) where I found a load of interesting information that had nothing to do with VS crashing, I finally decided to just try and open up the solution in Visual Studio 2010 Professional to see if it would crash that as well. It didn't. I made a couple of token changes to the code and project settings, built and saved everything, shut it down, and now I can open the solution again in Visual Studio Express without any error! Very strange. Don't know if this will help anybody as this seems a bit on the fringe of possibilities for this exception. I suspect there was an issue with my registry of all things as I've run into a few curious situations in the past with Visual Studio and my registry not getting along, all of those affecting how my projects load up, even forcing me to restore my registry, run a registry cleaner, and reboot to get normal behavior back again.