C#读写锁ReaderWriterLockSlim
https://blog.csdn.net/m0_37879526/article/details/109962340 我在CSDN上的博客,这里再补充一个
ReaderWriterLockSlim 读写锁
其实 ReaderWriterLockSlim 与 ReaderWriterLock 比较像
1.某个线程进入读取模式时,此时其他线程依然能进入读取模式。假设此时一个线程要进入写入模式,那么他不得不被阻塞。直到读取模式退出为止。
2.采用读写锁,则多个线程可以同时读取该对象,只有等到对象被写入锁占用的时候,才会阻塞。 写入锁占用时,读写锁都会被堵塞
3.如果某个线程进入了写入模式,那么其他线程无论是要写入还是读取,都是会被阻塞的。
进入写入/读取模式有2种方法:
EnterReadLock尝试进入写入模式锁定状态。
TryEnterReadLock(Int32) 尝试进入读取模式锁定状态,可以选择整数超时时间。
EnterWriteLock 尝试进入写入模式锁定状态。
TryEnterWriteLock(Int32) 尝试进入写入模式锁定状态,可以选择超时时间。
退出写入/读取模式有2种方法:
ExitReadLock 减少读取模式的递归计数,并在生成的计数为 0(零)时退出读取模式。
ExitWriteLock 减少写入模式的递归计数,并在生成的计数为 0(零)时退出写入模式。
public class Program { static private ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(); static void Main(string[] args) { Thread t_read1 = new Thread(new ThreadStart(ReadSomething)); t_read1.Start(); Console.WriteLine("{0} Create Thread ID {1} , Start ReadSomething", DateTime.Now.ToString("hh:mm:ss fff"), t_read1.GetHashCode()); Thread t_read2 = new Thread(new ThreadStart(ReadSomething)); t_read2.Start(); Console.WriteLine("{0} Create Thread ID {1} , Start ReadSomething", DateTime.Now.ToString("hh:mm:ss fff"), t_read2.GetHashCode()); Thread t_write1 = new Thread(new ThreadStart(WriteSomething)); t_write1.Start(); Console.WriteLine("{0} Create Thread ID {1} , Start WriteSomething", DateTime.Now.ToString("hh:mm:ss fff"), t_write1.GetHashCode()); } static public void ReadSomething() { Console.WriteLine("{0} Thread ID {1} Begin EnterReadLock...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); rwl.EnterReadLock(); try { Console.WriteLine("{0} Thread ID {1} reading sth...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); Thread.Sleep(5000);//模拟读取信息 Console.WriteLine("{0} Thread ID {1} reading end.", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); } finally { rwl.ExitReadLock(); Console.WriteLine("{0} Thread ID {1} ExitReadLock...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); } } static public void WriteSomething() { Console.WriteLine("{0} Thread ID {1} Begin EnterWriteLock...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); rwl.EnterWriteLock(); try { Console.WriteLine("{0} Thread ID {1} writing sth...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); Thread.Sleep(10000);//模拟写入信息 Console.WriteLine("{0} Thread ID {1} writing end.", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); } finally { rwl.ExitWriteLock(); Console.WriteLine("{0} Thread ID {1} ExitWriteLock...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); } } }
可以看到3号线程和4号线程能够同时进入读模式,而5号线程过了5秒钟后(即3,4号线程退出读锁后),才能进入写模式。
把上述代码修改一下,先开启2个写模式的线程,然后在开启读模式线程,代码如下:
static void Main(string[] args) { Thread t_write1 = new Thread(new ThreadStart(WriteSomething)); t_write1.Start(); Console.WriteLine("{0} Create Thread ID {1} , Start WriteSomething", DateTime.Now.ToString("hh:mm:ss fff"), t_write1.GetHashCode()); Thread t_write2 = new Thread(new ThreadStart(WriteSomething)); t_write2.Start(); Console.WriteLine("{0} Create Thread ID {1} , Start WriteSomething", DateTime.Now.ToString("hh:mm:ss fff"), t_write2.GetHashCode()); Thread t_read1 = new Thread(new ThreadStart(ReadSomething)); t_read1.Start(); Console.WriteLine("{0} Create Thread ID {1} , Start ReadSomething", DateTime.Now.ToString("hh:mm:ss fff"), t_read1.GetHashCode()); Thread t_read2 = new Thread(new ThreadStart(ReadSomething)); t_read2.Start(); Console.WriteLine("{0} Create Thread ID {1} , Start ReadSomething", DateTime.Now.ToString("hh:mm:ss fff"), t_read2.GetHashCode()); }
3号线程和4号线程都要进入写模式,但是3号线程先占用写入锁,因此4号线程不得不等了10s后才进入。5号线程和6号线程需要占用读取锁,因此等4号线程退出写入锁后才能继续下去。
TryEnterReadLock和TryEnterWriteLock可以设置一个超时时间,运行到这句话的时候,线程会阻塞在此,如果此时能占用锁,那么返回true,如果到超时时间还未占用锁,那么返回false,放弃锁的占用,直接继续执行下面的代码。
static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; //Console.WriteLine(Thread.CurrentThread.GetHashCode() + "begin"); //callMethod(); //Console.WriteLine(Thread.CurrentThread.GetHashCode() + "end"); Task[] tasks = new Task[5]; tasks[0] = Task.Factory.StartNew(WriteBook); tasks[1] = Task.Factory.StartNew(ReadBook); tasks[2] = Task.Factory.StartNew(WriteBook); tasks[3] = Task.Factory.StartNew(ReadBook); tasks[4] = Task.Factory.StartNew(WriteBook); //var task=Task.Factory.StartNew(ReadBook); //task.Start(); //Task.WaitAll(tasks); Console.ReadKey(); } static private ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(); // 测试 try超时 public static void ReadBook() { Console.WriteLine(Thread.CurrentThread.GetHashCode()+" Begin Read"); if (rwl.TryEnterReadLock(3000)) { try { Console.WriteLine(Thread.CurrentThread.GetHashCode() + " Enter Read"); Thread.Sleep(5000); Console.WriteLine(Thread.CurrentThread.GetHashCode() + "Read 获取读取锁成功"); Console.WriteLine(Thread.CurrentThread.GetHashCode() + "Read XXXXXXX"); } finally { rwl.ExitReadLock(); } } else { Console.WriteLine(Thread.CurrentThread.GetHashCode() + "Read 获取写入锁失败"); } Console.WriteLine(Thread.CurrentThread.GetHashCode()+" End Read"); } public static void WriteBook() { Console.WriteLine(Thread.CurrentThread.GetHashCode() + " Begin Write"); if (rwl.TryEnterWriteLock(3000)) { try { Console.WriteLine(Thread.CurrentThread.GetHashCode() + " Enter Write"); Thread.Sleep(1000); Console.WriteLine(Thread.CurrentThread.GetHashCode() + "Write 获取写入锁成功"); Console.WriteLine(Thread.CurrentThread.GetHashCode() + "Write XXXXXXX"); } finally { rwl.ExitWriteLock(); } } else { Console.WriteLine(Thread.CurrentThread.GetHashCode() + "Write 获取写入锁失败"); } Console.WriteLine(Thread.CurrentThread.GetHashCode() + " End Write"); }
EnterUpgradeableReadLock
ReaderWriterLockSlim类提供了可升级读模式,这种方式和读模式的区别在于它还有通过调用 EnterWriteLock 或 TryEnterWriteLock 方法升级为写入模式。 因为每次只能有一个线程处于可升级模式。进入可升级模式的线程,不会影响读取模式的线程,即当一个线程进入可升级模式,任意数量线程可以同时进入读取模式,不会阻塞。如果有多个线程已经在等待获取写入锁,那么运行EnterUpgradeableReadLock将会阻塞,直到那些线程超时或者退出写入锁。
static public void UpgradeableRead() { Console.WriteLine("{0} Thread ID {1} Begin EnterUpgradeableReadLock...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); rwl.EnterUpgradeableReadLock(); try { Console.WriteLine("{0} Thread ID {1} doing sth...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); Console.WriteLine("{0} Thread ID {1} Begin EnterWriteLock...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); rwl.EnterWriteLock(); try { Console.WriteLine("{0} Thread ID {1} writing sth...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); Thread.Sleep(10000);//模拟写入信息 Console.WriteLine("{0} Thread ID {1} writing end.", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); } finally { rwl.ExitWriteLock(); Console.WriteLine("{0} Thread ID {1} ExitWriteLock...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); } Thread.Sleep(10000);//模拟读取信息 Console.WriteLine("{0} Thread ID {1} doing end.", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); } finally { rwl.ExitUpgradeableReadLock(); Console.WriteLine("{0} Thread ID {1} ExitUpgradeableReadLock...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode()); } }
有一个官方的例子
private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim(); private Dictionary<int, string> innerCache = new Dictionary<int, string>(); public AddOrUpdateStatus AddOrUpdate(int key, string value) { cacheLock.EnterUpgradeableReadLock(); try { string result = null; if (innerCache.TryGetValue(key, out result)) { if (result == value) { return AddOrUpdateStatus.Unchanged; } else { cacheLock.EnterWriteLock(); try { innerCache[key] = value; } finally { cacheLock.ExitWriteLock(); } return AddOrUpdateStatus.Updated; } } else { cacheLock.EnterWriteLock(); try { innerCache.Add(key, value); } finally { cacheLock.ExitWriteLock(); } return AddOrUpdateStatus.Added; } } finally { cacheLock.ExitUpgradeableReadLock(); } }
递归
使用允许递归锁的构造方法(即new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion))构造ReaderWriterLockSlim实例
如何判断代码逻辑里会触发递归锁呢,抓住两个要点“同一线程(即ManageThreadId一样)”和“同一个锁”,在线程池里运行的时候,由于可以重用工作线程,很容易触发递归锁的问题,比如这样:线程池线程1获取到UpgradeableReadLock锁后由于后面代码里有长时等待任务,调度器可能调度线程1运行队列里的另一个同样代码的任务,这时由于代码一样线程1再次申请获取UpgradeableReadLock锁(因为上一次他已经获取到该UpgradeableReadLock锁了),就会报错了