如何使互斥体不递归

发布于 2024-08-15 02:43:08 字数 938 浏览 11 评论 0原文

我运行了下面的代码,期望在第二次锁定互斥体时锁定流程。运行两次后,我意识到它可以多次锁定(假设在同一个线程中)而不停止。我该如何改变这种行为?

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

using System.Threading;

namespace Test {
    class Program {
        static volatile Mutex mut1 = new Mutex();
        static volatile Mutex mut2 = new Mutex();
        static void Main(string[] args) {
            mut1.WaitOne(); Console.WriteLine("1W");
            mut2.WaitOne(); Console.WriteLine("2W");
            Thread oThread = new Thread(new ThreadStart(fn2));
            oThread.Start();
            mut1.WaitOne(); Console.WriteLine("1W");
            Console.WriteLine("Second");
            mut1.ReleaseMutex(); Console.WriteLine("1R");
        }
        static void fn2() {
            Console.WriteLine("First");
            mut2.ReleaseMutex(); Console.WriteLine("2R");
            mut1.ReleaseMutex(); Console.WriteLine("1R");
        }
    }
}

I ran the code below expecting flow to be locked on the 2nd time I lock a mutex. After running it twice I realize it can lock many times (assuming in the same thread) without stopping. How do I change this behavior?

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

using System.Threading;

namespace Test {
    class Program {
        static volatile Mutex mut1 = new Mutex();
        static volatile Mutex mut2 = new Mutex();
        static void Main(string[] args) {
            mut1.WaitOne(); Console.WriteLine("1W");
            mut2.WaitOne(); Console.WriteLine("2W");
            Thread oThread = new Thread(new ThreadStart(fn2));
            oThread.Start();
            mut1.WaitOne(); Console.WriteLine("1W");
            Console.WriteLine("Second");
            mut1.ReleaseMutex(); Console.WriteLine("1R");
        }
        static void fn2() {
            Console.WriteLine("First");
            mut2.ReleaseMutex(); Console.WriteLine("2R");
            mut1.ReleaseMutex(); Console.WriteLine("1R");
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-08-22 02:43:08

首先,我不确定您是否真的了解互斥体,您只能在它们最初被锁定的上下文(即线程)中释放它们,因此将它们用作某种保护点没有什么意义。

在此使用 信号量 可能更有意义案件。但你仍然应该弄清楚你真正想做的是什么:)

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

using System.Threading;

namespace Test
{
    class Program
    {
        static Semaphore sem1 = new Semaphore(1, 1);
        static Semaphore sem2 = new Semaphore(1, 1);
        static void Main(string[] args)
        {           
            sem1.WaitOne(); Console.WriteLine("1W");
            sem2.WaitOne(); Console.WriteLine("2W");
            Thread oThread = new Thread(new ThreadStart(fn2));
            oThread.Start();
            sem1.WaitOne(); Console.WriteLine("1W");
            Console.WriteLine("Second");
            sem1.Release(); Console.WriteLine("1R");
        }
        static void fn2()
        {
            Console.WriteLine("First");
            sem2.Release(); Console.WriteLine("2R");
            sem1.Release(); Console.WriteLine("1R");
        }
    }
}

For a start I am not sure you really understand mutexes, you can only release them in the context (i.e. thread) in which they were locked to begin with, so using them as some sort of guard points makes little sense.

It might make more sense to use semaphores in this case. But you still should work out what you are really trying to do :)

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

using System.Threading;

namespace Test
{
    class Program
    {
        static Semaphore sem1 = new Semaphore(1, 1);
        static Semaphore sem2 = new Semaphore(1, 1);
        static void Main(string[] args)
        {           
            sem1.WaitOne(); Console.WriteLine("1W");
            sem2.WaitOne(); Console.WriteLine("2W");
            Thread oThread = new Thread(new ThreadStart(fn2));
            oThread.Start();
            sem1.WaitOne(); Console.WriteLine("1W");
            Console.WriteLine("Second");
            sem1.Release(); Console.WriteLine("1R");
        }
        static void fn2()
        {
            Console.WriteLine("First");
            sem2.Release(); Console.WriteLine("2R");
            sem1.Release(); Console.WriteLine("1R");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文