如何使用C#读写锁ReaderWriterLockSlim
读写锁的概念很简单,允许多个线程同时获取读锁,但同一时间只允许一个线程获得写锁,因此也称作共享-独占锁。在c#中,推荐使用readerwriterlockslim类来完成读写锁的功能。
某些场合下,对一个对象的读取次数远远大于修改次数,如果只是简单的用lock方式加锁,则会影响读取的效率。而如果采用读写锁,则多个线程可以同时读取该对象,只有等到对象被写入锁占用的时候,才会阻塞。
简单的说,当某个线程进入读取模式时,此时其他线程依然能进入读取模式,假设此时一个线程要进入写入模式,那么他不得不被阻塞。直到读取模式退出为止。
同样的,如果某个线程进入了写入模式,那么其他线程无论是要写入还是读取,都是会被阻塞的。
进入写入/读取模式有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,放弃锁的占用,直接继续执行下面的代码。
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()); } }
读写锁对于性能的影响是明显的。
下面测试代码:
public class program { static private readerwriterlockslim rwl = new readerwriterlockslim(); static void main(string[] args) { stopwatch sw = new stopwatch(); sw.start(); list<task> lsttask = new list<task>(); for (int i = 0; i < 500; i++) { if (i % 25 != 0) { var t = task.factory.startnew(readsomething); lsttask.add(t); } else { var t = task.factory.startnew(writesomething); lsttask.add(t); } } task.waitall(lsttask.toarray()); sw.stop(); console.writeline("使用readerwriterlockslim方式,耗时:" + sw.elapsed); sw.restart(); lsttask = new list<task>(); for (int i = 0; i < 500; i++) { if (i % 25 != 0) { var t = task.factory.startnew(readsomething_lock); lsttask.add(t); } else { var t = task.factory.startnew(writesomething_lock); lsttask.add(t); } } task.waitall(lsttask.toarray()); sw.stop(); console.writeline("使用lock方式,耗时:" + sw.elapsed); } static private object _lock1 = new object(); static public void readsomething_lock() { lock (_lock1) { //console.writeline("{0} thread id {1} reading sth...", datetime.now.tostring("hh:mm:ss fff"), thread.currentthread.gethashcode()); thread.sleep(10);//模拟读取信息 //console.writeline("{0} thread id {1} reading end.", datetime.now.tostring("hh:mm:ss fff"), thread.currentthread.gethashcode()); } } static public void writesomething_lock() { lock (_lock1) { //console.writeline("{0} thread id {1} writing sth...", datetime.now.tostring("hh:mm:ss fff"), thread.currentthread.gethashcode()); thread.sleep(100);//模拟写入信息 //console.writeline("{0} thread id {1} writing end.", datetime.now.tostring("hh:mm:ss fff"), thread.currentthread.gethashcode()); } } static public void readsomething() { rwl.enterreadlock(); try { //console.writeline("{0} thread id {1} reading sth...", datetime.now.tostring("hh:mm:ss fff"), thread.currentthread.gethashcode()); thread.sleep(10);//模拟读取信息 //console.writeline("{0} thread id {1} reading end.", datetime.now.tostring("hh:mm:ss fff"), thread.currentthread.gethashcode()); } finally { rwl.exitreadlock(); } } static public void writesomething() { rwl.enterwritelock(); try { //console.writeline("{0} thread id {1} writing sth...", datetime.now.tostring("hh:mm:ss fff"), thread.currentthread.gethashcode()); thread.sleep(100);//模拟写入信息 //console.writeline("{0} thread id {1} writing end.", datetime.now.tostring("hh:mm:ss fff"), thread.currentthread.gethashcode()); } finally { rwl.exitwritelock(); } } }
上述代码,就500个task,每个task占用一个线程池线程,其中20个写入线程和480个读取线程,模拟操作。其中读取数据花10ms,写入操作花100ms,分别测试了对于lock方式和readerwriterlockslim方式。可以做一个估算,对于readerwriterlockslim,假设480个线程同时读取,那么消耗10ms,20个写入操作占用2000ms,因此所消耗时间2010ms,而对于普通的lock方式,由于都是独占性的,因此480个读取操作占时间4800ms+20个写入操作2000ms=6800ms。运行结果显示了性能提升明显。
以上是本文的全部内容,希望对大家熟练应用读写锁readerwriterlockslim有所帮助。
上一篇: 怎么下载超清MP4格式的虎妈猫爸?
下一篇: 狗肉的做法大全有哪些!