c# – 一种主/从锁定系统?

我不知道我想要做的事情是否有名字.

令人遗憾的是,“主/从锁定系统”是我能想到的最好的措辞.

现在我遇到的问题……

想象一下,你有以下课程:

public class Foo
{
    public void Master()
    {

    }

    public void Slave1()
    {

    }

    public void Slave2()
    {

    }
}

我想要的是从属方法(Slave1,Slave2)可以在多线程场景中并行运行,但是当调用master(Master)方法时,Slaves方法应该在执行时被阻止执行,另外所有当前正在运行进入主方法后,从属方法应运行至完成.

像这样的东西(带评论):

public class Foo
{
    public void Master()
    {
        //block slaves from executing
        //wait for slaves to finish

        //do code...

        //unblock slaves
    }

    public void Slave1()
    {
        //if blocked by master wait, if not execute
        //if currently running when entering master method, let it finish
    }

    public void Slave2()
    {
        //if blocked by master wait, if not execute
        //if currently running when entering master method, let it finish
    }
}

我知道我可以在所有3种方法上使用锁定,但Slave1方法会阻塞对方,这不是我想要的.

public class Foo
{
    private readonly object _syncLock = new object();

    public void Master()
    {
        lock (_syncLock) //blocks Slave1, Slave2
        {
            //run code...
        }
    }

    public void Slave1()
    {
        lock (_syncLock) //blocks Slave2, Master - dont want that
        {
            //run code...
        }
    }

    public void Slave2()
    {
        lock (_syncLock) //blocks Slave1, Master - dont want that
        {
            //run code...
        }
    }
}

如果可能的话,我希望在这个类中有解决方案而不是一些外部的“如果你按照它的方式调用方法”,所提到的方法可以在任何时候以非有序的方式触发,并且每个方法可以运行多个时间.

最佳答案 如果我理解你,你想要

> Master()上的独占(写)锁(没有SlaveN可以运行)
>每个Slave上的共享(读取)锁定(您可以运行另一个SlaveN,但不能运行Master)

如果是你的情况,请看看ReaderWriterLockSlim

public class Foo {
    private readonly ReaderWriterLockSlim _syncLock = new ReaderWriterLockSlim();

    public void Master() {
      // Exclusive (write) lock: only Master allowed to run
      _syncLock.EnterWriteLock();

      try {
        //run code...
      }
      finally {
        _syncLock.ExitWriteLock();
      }   
    }

    public void Slave1() {
      // Read lock: you can run Slave2 (with another Read lock), but not Master 
      _syncLock.EnterReadLock();

      try {
        //run code...
      }
      finally {
        _syncLock.ExitReadLock();
      }         
    }

    public void Slave2() {
      // Read lock: you can run Slave1 (with another Read lock), but not Master 
      _syncLock.EnterReadLock();

      try {
        //run code...
      }
      finally {
        _syncLock.ExitReadLock();
      }         
    }   
}
点赞