Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#互斥锁死锁_C#_Mutex - Fatal编程技术网

c#互斥锁死锁

c#互斥锁死锁,c#,mutex,C#,Mutex,我们有以下代码: public static Mutex AcquireMutex(string name) { bool ndgMutexAcquired; Mutex ndgMutex = new Mutex(true, name, out ndgMutexAcquired); if (!ndgMutexAcquired) { tr

我们有以下代码:

        public static Mutex AcquireMutex(string name)
        {
            bool ndgMutexAcquired;
            Mutex ndgMutex = new Mutex(true, name, out ndgMutexAcquired);

            if (!ndgMutexAcquired)
            {
                try
                {
                    ndgMutex.WaitOne();
                }
                catch (AbandonedMutexException)
                {
                }
            }

            return ndgMutex;
        }
我有一个父进程,在任务中,我调用AcquireMutex,运行一些代码,然后释放它。在另一个任务中,我调用一个子进程,它也使用相同的名称调用AcquireMutex,然后立即调用release。这似乎是死锁。我唯一认为的是,“初始拥有”参数可能需要为false,如下所示:

        public static Mutex AcquireMutex(string name)
        {
            Mutex ndgMutex = new Mutex(false, name);
            try
            {
                ndgMutex.WaitOne();
            }
            catch (AbandonedMutexException)
            {
            }

            return ndgMutex;
        }
我真的不明白为什么那样会有帮助,但似乎有帮助。有人能帮我理解为什么会出现死锁,正确的解决方法是什么吗

下面是在子进程中调用Acquire mutex的代码

            using (var mutex = MutexExtensions.AcquireMutex(@"Global\auth_mutex"))
            {
                LogHelper.LogInfo("Releasing mutex since it was successfully acquired");
                mutex.ReleaseMutex();
            }
在父进程中:

            return Task.Run(() =>
            {
                Logger.LogInfo("Acquiring mutex ...");
                using (var mutex = MutexExtensions.AcquireMutex(@"Global\auth_mutex"))
                {
                    Logger.LogInfo("Auth mutex acquired");
                    // Stuff done here (external process call) ~~~~~~~~~~~~

                    // Mutex.Dispose doesn't implicitly release the mutex
                    Logger.LogInfo("Releasing mutex");
                    mutex.ReleaseMutex();

                    return new TaskResult(true);
                }
            });

你在哪里处理互斥锁?此外,您并不打算创建这样的新互斥体(您可以,但只有在您知道自己在做什么的情况下)-一般来说,
互斥体
对象应该是
静态只读
。最重要的是:永远不要吞下一个
放弃的mutexception
!!!。Microsoft在文档中非常清楚地说明了这一点:这是用于在单独的进程上进行同步,还是仅用于单个进程?正如我的蜘蛛感觉告诉我的,你可能根本不想要互斥体……你所说的“子进程”是什么意思?(这不是Windows中使用的术语),您是指真正的子进程
进程
(实际上由父进程创建的
进程
,而不使用
ShellExecute
),还是仅仅是一些代码块?你说的“在另一个任务中”到底是什么意思?(不要忘记async
Task
通常在不同的线程上运行,并且
互斥体
有从不同线程使用它的特定规则),请发布尝试获取互斥体的其他代码-您只发布了其中的一半。我正在pinvoking CreateProcess以创建子进程(我这样做是为了让子进程能够正确地写入控制台),而子进程正在调用这个方法