本文介绍了这是为什么程序错误? `对象同步方法从code`未同步块称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是不对的code?我碰到一个对象的同步方法从code不同步的块被称为。我发现一个结果,对谷歌的说,我可能会释放互斥锁,但根据我的输出在此之前并非如此。这里是没有其他code之间的互斥code。

-edit-对不起你们,错贴。

我的输出

  1W
1W
2W
 

code

 使用系统;
使用System.Collections.Generic;
使用System.Text;
使用的System.Threading;

命名空间sqliteTest
{
    类节目
    {
        静态挥发互斥MUT1 =新的mutex();
        静态挥发互斥MUT2 =新的mutex();
        静态无效的主要(字串[] args)
        {
            mut1.WaitOne(); Console.WriteLine(1W);
            螺纹oThread =新主题(新的ThreadStart(FN2));
            oThread.Start();
            mut1.WaitOne(); Console.WriteLine(1W);
            更新(0);
        }
        静态无效FN2()
        {
            mut2.WaitOne(); Console.WriteLine(2W);
            mut1.ReleaseMutex(); Console.WriteLine(1R);
            mut2.WaitOne(); Console.WriteLine(2W);
            更新(1);
            mut1.ReleaseMutex(); Console.WriteLine(1R);
        }
        静态无效更新(INT T)
        {
            mut2.ReleaseMutex(); Console.WriteLine(2R);
            如果(T == 0)
            {
                mut1.WaitOne();
                Console.WriteLine(1W);
            }
        }
    }
}
 

解决方案

这是不是一个很大的错误信息时,Windows会产生它。它真正的意思是,你是对的,你没有自己的互斥调用ReleaseMutex。你会得到过去的第一异常

 静态挥发互斥MUT2 =新的mutex(真正的);
 

但随后它会死的线程内,当它调用ReleaseMutex上MUT1,它并不拥有。不知道你想做什么,在code没有多大意义了我。

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");
            }
        }
    }
}
解决方案

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.

这篇关于这是为什么程序错误? `对象同步方法从code`未同步块称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:11