溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#讀寫鎖ReaderWriterLockSlim的使用

發(fā)布時間:2020-06-21 20:39:20 來源:網(wǎng)絡(luò) 閱讀:7626 作者:cnn237111 欄目:編程語言

讀寫鎖的概念很簡單,允許多個線程同時獲取讀鎖,但同一時間只允許一個線程獲得寫鎖,因此也稱作共享-獨占鎖。在C#中,推薦使用ReaderWriterLockSlim類來完成讀寫鎖的功能。

某些場合下,對一個對象的讀取次數(shù)遠遠大于修改次數(shù),如果只是簡單的用lock方式加鎖,則會影響讀取的效率。而如果采用讀寫鎖,則多個線程可以同時讀取該對象,只有等到對象被寫入鎖占用的時候,才會阻塞。

簡單的說,當某個線程進入讀取模式時,此時其他線程依然能進入讀取模式,假設(shè)此時一個線程要進入寫入模式,那么他不得不被阻塞。直到讀取模式退出為止。

同樣的,如果某個線程進入了寫入模式,那么其他線程無論是要寫入還是讀取,都是會被阻塞的。

進入寫入/讀取模式有2種方法:

EnterReadLock嘗試進入寫入模式鎖定狀態(tài)。

TryEnterReadLock(Int32) 嘗試進入讀取模式鎖定狀態(tài),可以選擇整數(shù)超時時間。

EnterWriteLock 嘗試進入寫入模式鎖定狀態(tài)。

TryEnterWriteLock(Int32) 嘗試進入寫入模式鎖定狀態(tài),可以選擇超時時間。

退出寫入/讀取模式有2種方法:

ExitReadLock 減少讀取模式的遞歸計數(shù),并在生成的計數(shù)為 0(零)時退出讀取模式。

ExitWriteLock 減少寫入模式的遞歸計數(shù),并在生成的計數(shù)為 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());
            }
        }
    }

C#讀寫鎖ReaderWriterLockSlim的使用

可以看到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());
        }

結(jié)果如下:

C#讀寫鎖ReaderWriterLockSlim的使用

可以看到,3號線程和4號線程都要進入寫模式,但是3號線程先占用寫入鎖,因此4號線程不得不等了10s后才進入。5號線程和6號線程需要占用讀取鎖,因此等4號線程退出寫入鎖后才能繼續(xù)下去。

TryEnterReadLock和TryEnterWriteLock可以設(shè)置一個超時時間,運行到這句話的時候,線程會阻塞在此,如果此時能占用鎖,那么返回true,如果到超時時間還未占用鎖,那么返回false,放棄鎖的占用,直接繼續(xù)執(zhí)行下面的代碼。

EnterUpgradeableReadLock

ReaderWriterLockSlim類提供了可升級讀模式,這種方式和讀模式的區(qū)別在于它還有通過調(diào)用 EnterWriteLock 或 TryEnterWriteLock 方法升級為寫入模式。 因為每次只能有一個線程處于可升級模式。進入可升級模式的線程,不會影響讀取模式的線程,即當一個線程進入可升級模式,任意數(shù)量線程可以同時進入讀取模式,不會阻塞。如果有多個線程已經(jīng)在等待獲取寫入鎖,那么運行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個讀取線程,模擬操作。其中讀取數(shù)據(jù)花10ms,寫入操作花100ms,分別測試了對于lock方式和ReaderWriterLockSlim方式??梢宰鲆粋€估算,對于ReaderWriterLockSlim,假設(shè)480個線程同時讀取,那么消耗10ms,20個寫入操作占用2000ms,因此所消耗時間2010ms,而對于普通的lock方式,由于都是獨占性的,因此480個讀取操作占時間4800ms+20個寫入操作2000ms=6800ms。運行結(jié)果顯示了性能提升明顯。

C#讀寫鎖ReaderWriterLockSlim的使用

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI