ReaderWriterLockSlim类

为了使锁定机制允许锁定多个读取器(而不是一个写入器)访问某个资源,可以使用ReaderWriterLockSlim类。这个类提供了一个锁定功能,如果没有写入器锁定资源,就允许多个读取器访问资源,但只能有一个写入器锁定该资源。

ReaderWriterLockSlim类有阻塞或不阻塞的方法来获取读取锁,如阻塞的EnterReadLock()和不阻塞的TryEnterReadLock()方法,还可以使用阻塞的EnterWriteLock()和不阻塞的TryEnterWriteLock()方法获得写入锁定。如果任务先读取资源,之后写入资源,它就可以使用EnterUpgradableReadLock()或TryEnterUpgradableReadLock()方法获得可升级的读取锁定。有了这个锁定,就可以获得写入锁定,而不需要释放读取锁定。

这个类的几个属性提供了当前锁定的相关信息,如CurrentReadCount、WaitingReadCount、WaitingUpgradableReadCount和WaitingWriteCount。

下面的示例程序创建了一个包含6项的集合和一个ReaderWriterLockSlim()对象。ReaderMethod()方法获得一个读取锁定,读取列表中的所有项,并把它们写到控制台中。WriterMethod()方法试图获得一个写入锁定,以改变集合的所有值。在Main()方法中,启动6个任务,以调用ReaderMethod()或WriteMethod()方法。

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using static System.Console;

namespace ReaderWriterSample
{
    class Program
    {
        private static List<int> _items = new List<int>() { 0,1,2,3,4,5};
        private static ReaderWriterLockSlim _rwl = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
        static void Main(string[] args)
        {
            var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning,TaskContinuationOptions.None);
            var tasks = new Task[6];
            tasks[0] = taskFactory.StartNew(WriterMethod,1);
            tasks[1] = taskFactory.StartNew(ReaderMethod,1);
            tasks[2] = taskFactory.StartNew(ReaderMethod,2);
            tasks[3] = taskFactory.StartNew(WriterMethod,2);
            tasks[4] = taskFactory.StartNew(ReaderMethod,3);
            tasks[5] = taskFactory.StartNew(ReaderMethod,4);
            Task.WaitAll(tasks);
            ReadLine();
        }
        static void ReaderMethod(object reader)
        {
            try
            {
                _rwl.EnterReadLock();
                for (int i = 0; i < _items.Count; i++)
                {
                    WriteLine($"reader {reader}, loop: {i},itme: {_items[i]}");
                    Task.Delay(40).Wait();
                }
            }
            finally
            {
                _rwl.ExitReadLock();
            }
        }
        static void WriterMethod(object writer)
        {
            try
            {
                while (!_rwl.TryEnterWriteLock(50))
                {
                    WriteLine($"Writer {writer} waiting for the write lock");
                    WriteLine($"current reader count: {_rwl.CurrentReadCount}");
                }
                WriteLine($"Writer {writer} acquired the lock");
                for (int i = 0; i < _items.Count; i++)
                {
                    _items[i]++;
                    Task.Delay(50).Wait();
                }
                WriteLine($"Writer {writer} finished");
            }
            finally
            {
                _rwl.ExitWriteLock();
            }
        }
    }
}

执行这个应用程序,可以看到第1个写入器先获得锁定。第2个写入器和所有的读取器需要等待。接着,第2个写入器也获得锁定,所有读取器仍在等待,最后读取器同时工作。

输出结果:

Writer 1 acquired the lock
Writer 2 waiting for the write lock
current reader count: 0
Writer 2 waiting for the write lock
current reader count: 0
Writer 2 waiting for the write lock
current reader count: 0
Writer 2 waiting for the write lock
current reader count: 0
Writer 2 waiting for the write lock
current reader count: 0
Writer 2 waiting for the write lock
current reader count: 0
Writer 2 waiting for the write lock
current reader count: 0
Writer 1 finished
Writer 2 acquired the lock
Writer 2 finished
reader 3, loop: 0,itme: 2
reader 4, loop: 0,itme: 2
reader 1, loop: 0,itme: 2
reader 2, loop: 0,itme: 2
reader 4, loop: 1,itme: 3
reader 3, loop: 1,itme: 3
reader 2, loop: 1,itme: 3
reader 1, loop: 1,itme: 3
reader 4, loop: 2,itme: 4
reader 2, loop: 2,itme: 4
reader 3, loop: 2,itme: 4
reader 1, loop: 2,itme: 4
reader 4, loop: 3,itme: 5
reader 3, loop: 3,itme: 5
reader 1, loop: 3,itme: 5
reader 2, loop: 3,itme: 5
reader 2, loop: 4,itme: 6
reader 1, loop: 4,itme: 6
reader 3, loop: 4,itme: 6
reader 4, loop: 4,itme: 6
reader 4, loop: 5,itme: 7
reader 2, loop: 5,itme: 7
reader 1, loop: 5,itme: 7
reader 3, loop: 5,itme: 7

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读