English 中文(简体)
例外:一致反对方法来自不同步的法典体
原标题:Exception : Object synchronization method was called from an unsynchronized block of code

I have several threads that write to the same int. Each thread increment the integer value. What is the simple way to synchronize the increment operation. The lock statement works only on Object so i can t use it. I tried also the following:

static int number=0;

static void Main(string[] args)
    {
        ThreadStart ts = new ThreadStart(strtThread);
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        Console.ReadLine();
    }

    public static void strtThread()
    {
        bool lockTaken = false;

        Monitor.Enter(number,ref lockTaken);
        try
        {
            Random rd = new Random();
            int ee = rd.Next(1000);
            Console.WriteLine(ee);
            Thread.Sleep(ee);
            number++;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally 
        {
            if (lockTaken)
            {
             Monitor.Exit(number);
            }

        }
    }

它给我留下以下错误:

提出反对的同步方法来自不同步的法典体。

最佳回答

You can use the Interlocked.Increment Method to automically increment an integer without locking:

public static void strtThread()
{
    Interlocked.Increment(ref number);
}

If you have multiple statements, you can create an object instance that you can lock:

private static int number = 0;
private static readonly object gate = new object();

public static void strtThread()
{
    lock (gate)
    {
       number++;
    }    
}
问题回答

你们为什么会加入<条码>。 我认为,这是个问题。 为此:

static Object locking = new Object();

static void Main(string[] args)
    {
        ThreadStart ts = new ThreadStart(strtThread);
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        Console.ReadLine();
    }

    public static void strtThread()
    {
        lock(locking) {
            try
            {
                Random rd = new Random();
                int ee = rd.Next(1000);
                Console.WriteLine(ee);
                Thread.Sleep(ee);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签