C# 如何创建自定义互斥锁

C# 如何创建自定义互斥锁,c#,mutex,C#,Mutex,面试问题: 创建您自己的互斥体: 我的实施: 公共静态类MyMutex { 静态队列=新队列(); 静态int锁定_状态=0; 静态int-internal_-proc=0; 公共静态void WaitOne() { //旋转循环直到内部过程完成 而(联锁等于(内部程序,1)) {} 联锁交换(参考内部程序1); //如果处于锁定状态,请将当前线程排队并设置为inifinite sleep if(联锁交换(参考锁定状态,1)==1) { 排队(Thread.CurrentThread); 睡眠(

面试问题:

创建您自己的互斥体:

我的实施:

公共静态类MyMutex {

静态队列=新队列();
静态int锁定_状态=0;
静态int-internal_-proc=0;
公共静态void WaitOne()
{
//旋转循环直到内部过程完成
而(联锁等于(内部程序,1))
{}
联锁交换(参考内部程序1);
//如果处于锁定状态,请将当前线程排队并设置为inifinite sleep
if(联锁交换(参考锁定状态,1)==1)
{
排队(Thread.CurrentThread);
睡眠(-1);
}
联锁交换(参考内部程序,0);
}
公共静态void ReleaseMutex()
{
//旋转循环直到内部过程完成
而(联锁等于(内部程序,1))
{}
//锁定内部进程(更改为队列)
联锁交换(参考内部程序1);
如果(队列计数>0)
{
线程t=queue.Dequeue();
t、 Start();
}
如果(queue.Count==0)
{
联锁交换(参考锁定状态,0);
}
//结束锁内部进程(更改为队列)
联锁交换(参考内部程序,0);
}
}
解释:

如果互斥锁处于锁定状态,线程将排队,然后线程将进入睡眠模式无限长的时间跨度。 检查和赋值是以原子方式进行的,因此进入的第一个线程将“锁定” 在任何其他人获得机会之前的州(有一面1号旗)

问题是,当一个线程解队列时,另一个线程可以进入并调用waitOne(),然后锁定的状态可能被伪装为0; 因此,我有一个内部自旋循环,它阻止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.
}

至少要回答您的部分问题,以下是我如何处理暂停/恢复线程:

// 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.
}

如何重新发明轮子是一个糟糕的面试问题。他们给了你什么指导方针?您可以使用线程锁吗,或者它必须是“纯的”(无论在托管代码中是什么意思)?如果可以使用线程锁,至少可以通过这种方式避免多线程问题。。。不确定这是否是作弊;)在这里,使方法
静态
对您没有帮助。我不能使用锁,那将是错误的cheating@Jeff是的,我知道我以前有别的想法,但在这里对我没有伤害,所以我就这样离开了。如何重新发明轮子是一个糟糕的面试问题。他们给了你什么指导方针?您可以使用线程锁吗,或者它必须是“纯的”(无论在托管代码中是什么意思)?如果可以使用线程锁,至少可以通过这种方式避免多线程问题。。。不确定这是否是作弊;)在这里,使方法
静态
对您没有帮助。我不能使用锁,那将是错误的cheating@Jeff是的,我知道我以前有别的想法,但在这里对我没有伤害,所以我就这样离开了。