C#读写锁ReaderWriterLockSlim的使用(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());
        }

结果如下:

C#读写锁ReaderWriterLockSlim的使用

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

读写锁对于性能的影响是明显的。

下面测试代码:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3d24294803b8ba800ef11cc4b0748179.html