讀寫鎖的概念很簡單,允許多個線程同時獲取讀鎖,但同一時間只允許一個線程獲得寫鎖,因此也稱作共享-獨占鎖。在C#中,推薦使用ReaderWriterLockSlim類來完成讀寫鎖的功能。
某些場合下,對一個對象的讀取次數遠遠大於修改次數,如果只是簡單的用lock方式加鎖,則會影響讀取的效率。而如果采用讀寫鎖,則多個線程可以同時讀取該對象,只有等到對象被寫入鎖占用的時候,才會阻塞。
簡單的說,當某個線程進入讀取模式時,此時其他線程依然能進入讀取模式,假設此時一個線程要進入寫入模式,那麼他不得不被阻塞。直到讀取模式退出為止。
C#多線程編程實例 線程與窗體交互【附源碼】 http://www.linuxidc.com/Linux/2014-07/104294.htm
C#數學運算表達式解釋器 http://www.linuxidc.com/Linux/2014-07/104289.htm
在C語言中解析JSON配置文件 http://www.linuxidc.com/Linux/2014-05/101822.htm
C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm
同樣的,如果某個線程進入了寫入模式,那麼其他線程無論是要寫入還是讀取,都是會被阻塞的。
進入寫入/讀取模式有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個寫模式的線程,然後在開啟讀模式線程,代碼如下:
{
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。運行結果顯示了性能提升明顯。