简体   繁体   English

使用Mutex同步C#对象:“从未同步的代码块调用了对象同步方法”错误

[英]Use Mutex to synchronize C# object: “Object synchronization method was called from an unsynchronized block of code” error

I have a C# application code in which I use a mutex to synchronise some code during the creation of an object. 我有一个C#应用程序代码,其中在创建对象的过程中使用互斥锁来同步某些代码。 The object constructor acquires the mutex and ONLY releases it when the object is no longer needed (during app shutdown). 对象构造函数获取互斥量,并且仅在不再需要该对象时(在应用关闭期间)才释放互斥量。 Thus one place to release the mutex would be in the object destructor. 因此,释放互斥量的一个地方就是对象析构函数。 The problem that arose is that sometimes I got an exception during the call to ReleaseMutex() in the object destructor. 出现的问题是,有时在对象析构函数中的ReleaseMutex()调用期间出现异常。 The exception is: "Object synchronization method was called from an unsynchronized block of code". 例外是:“从未同步的代码块调用了对象同步方法”。 It appears that the thread that does gabage collection which calls the object destructor sometimes is not the same thread that waits for the mutex (Mutex.WaitOne(false, namedMutex)) in the first place. 看起来,进行垃圾回收的线程有时调用对象析构函数,该线程与等待互斥对象(Mutex.WaitOne(false,namedMutex))的线程不同。 How do I syncrhonize the acquire and release of the mutex on the same thread to avert this exception? 如何在同一个线程上同步获取和释放互斥锁,以避免此异常? Thanks for your help! 谢谢你的帮助!

public class MyObject
{
    static ExtDeviceDriver devDrv;
    private Mutex mut = new Mutex(false,myMutex);

    public MyObject()
    {
        mut.WaitOne();
        //Thread safe code here.
        devDrv = new ExtDeviceDriver();
    }

    ~MyObject()
    {
        mut.ReleaseMutex();
    }
}

Why don't you use the Dispose pattern ? 为什么不使用Dispose模式 Mutex inherits from WaitHandle and WaitHandle implements IDisposable . Mutex继承自WaitHandleWaitHandle实现IDisposable If you have a class that creates and uses an IDisposable it should also implement IDisposable and properly be disposed of. 如果您有一个创建并使用IDisposable的类,则它也应实现IDisposable并进行适当处理。 Don't let the GC dispose of your mutex for you, manually do it in a using block or manually call .Dispose() on it. 不要让GC为您处理互斥体,而是在using块中手动进行操作,或在其上手动调用.Dispose() This way you can always know who is doing what and when. 这样,您始终可以知道谁在做什么以及何时在做什么。

This post has a great quote that you should take to heart: 这篇文章有一个不错的报价,您应该铭记于心:

If the object implements IDisposable then you should think about how the object is getting cleaned up. 如果对象实现IDisposable,则应考虑如何清理对象。

Objects that implement IDisposable usually do so because they are holding on to real resources that should be freed deterministically. 实现IDisposable的对象之所以这样做,是因为它们持有应确定释放的真实资源。

Which is exactly what is happening here. 这正是这里正在发生的事情。

Also I see you are using a named mutex. 另外,我看到您正在使用命名的互斥锁。 Named mutexes are used across processes and are managed as operating system handles. 命名互斥锁在各个进程中使用,并作为操作系统句柄进行管理。 Is anyone else acquiring the same mutex and trying to release it? 是否有其他人获得相同的互斥锁并试图释放它? Is there a reason you need a named mutex? 您是否需要命名互斥锁的原因? These are usually tricky to deal with because you can have abandanded mutexes and all sorts of other weird stuff if the process dies and the mutex isn't gracefully handled. 这些通常很难处理,因为如果进程死了并且互斥量未得到妥善处理,您可能会放弃互斥量以及各种其他奇怪的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 信号量 C# Object 同步方法是从未同步的代码块调用的 - semaphores C# Object synchronization method was called from an unsynchronized block of code 对象同步方法是从未同步的代码块中调用的。 Mutex.Release() 上的异常 - Object synchronization method was called from an unsynchronized block of code. Exception on Mutex.Release() 代码抛出 Object 同步方法是从未同步的代码块中调用的 - code throws Object synchronization method was called from an unsynchronized block of code 异常:从未同步的代码块中调用了对象同步方法 - Exception : Object synchronization method was called from an unsynchronized block of code 从一个未同步的代码块中调用了对象同步方法 - Object synchronization method was called from an unsynchronized block of code Unity Container-从未同步的代码块调用对象同步方法 - Unity Container - Object synchronization method was called from an unsynchronized block of code 从不同步的代码块调用Monitor和Object同步方法 - Monitor and Object synchronization method was called from an unsynchronized block of code 为什么这个程序出错? `对象同步方法是从一个不同步的代码块调用的 - Why is this program in error? `Object synchronization method was called from an unsynchronized block of code` 回调测试中的“从不同步的代码块调用对象同步方法”错误 - "Object synchronization method was called from an unsynchronized block of code" error in Callback test C#中的互斥量-来自未同步代码块的同步 - Mutex in C# - Synchronization from Unsynchronized Block of Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM