c# - 如何创建自定义互斥体

标签 c# mutex

<分区>

面试问题:

创建你自己的互斥锁:

我的实现:

public static class MyMutex {

    static Queue<Thread> queue = new Queue<Thread>();

    static int locked_state = 0 ;
    static int inner_proc = 0;

    public static void WaitOne()
    {
        // spin loop untill inner proccess are complete 
        while (Interlocked.Equals(inner_proc ,1))
        {}

        Interlocked.Exchange(ref inner_proc, 1);
        // if in a locked state queue current thread and set to inifinite sleep 
        if (Interlocked.Exchange(ref locked_state, 1) == 1)
        {
            queue.Enqueue(Thread.CurrentThread);
            Thread.Sleep(-1);
        }

        Interlocked.Exchange(ref inner_proc, 0);
    }
    public static void ReleaseMutex()
    {
        // spin loop untill inner proccess are complete 
        while (Interlocked.Equals(inner_proc ,1))
        {}
        // lock inner process (change to queue)            
        Interlocked.Exchange(ref inner_proc, 1);

        if( queue.Count > 0 )
        {
            Thread t = queue.Dequeue();
            t.Start();                 
        }
        if (queue.Count == 0)
        {
            Interlocked.Exchange(ref locked_state, 0);
        }
        // end lock inner process ( change to queue )            
        Interlocked.Exchange(ref inner_proc, 0);
    }
}

解释:

如果互斥量处于锁定状态,则线程将排队,然后线程进入无限时间跨度的 sleep 模式。 检查和分配是自动进行的,以便第一个进入的线程将“锁定” 在任何其他人获得机会之前的状态(带有 1 标志)。

问题是当一个线程出队时,另一个线程可能会在 locked_state 被标记为 0 之前进入并调用 waitOne() ; 出于这个原因,我有一个内部自旋循环,它阻止 2 个线程同时改变队列。

*另一个问题是我怎样才能让线程休眠并唤醒它,就像我尝试的那样 在这里做(但我不能像我那样使用 thread.Start() 它会抛出异常) 并且线程挂起和恢复已被弃用。

所以一般来说(我真的不知道如何实现互斥量) 任何有关如何实现此目的的提示、想法或有用的链接都将不胜感激。

最佳答案

为了至少回答你的部分问题,这里是我将如何处理暂停/恢复线程:

// In the thread to be suspended:
// This will return false if the thread needs to end immediately.
// This will return true if normal operation should continue.
private bool SuspendCurrentThread()
{
    try
    {
        for (;;)
        {
            Thread.Sleep(Timeout.Infinite);
        }
    }
    catch (ThreadInterruptedException)
    {
        // Resume normal operation.
        return true;
    }
    catch (ThreadAbortException)
    {
        return false;
    }
}

// ...
// ... 
// ...

// In the thread that is to trigger the resume:
private Thread OtherThread = /* something */;

private void ResumeOtherThread()
{
    OtherThread.Interrupt();
}

private void KillOtherThread()
{
    OtherThread.Abort(); // Fire a ThreadAbortException in the other thread if it is in a waiting state.
    OtherThread.Join(); // Wait until the other thread has exited before continuing.
}

关于c# - 如何创建自定义互斥体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9138277/

相关文章:

c - 如何在 FreeBSD/Mac OS X 中从 Mutex 对象获取拥有的线程 ID

c# - 统一平稳运动

c++ - shared_mutex 锁排序

互斥体上的 C++ 并发段错误

c# - 字符串和数字条件的最佳实践

rust - 操作系统线程调度是否会影响使用 std::sync::Mutex 与 tokio::sync::Mutex 的决定?

c++ - 我们需要在 C++ 中同步局部对象变量吗?

c# - 配置 Enterprise Library 5.0 数据访问应用程序 block

c# - 编译错误集合被修改

c# - NHibernate 使用 Linq 查询字符串集合导致错误或空集合