当前位置: 首页>>代码示例>>C#>>正文


C# ReaderWriterLockSlim.UsingReadLock方法代码示例

本文整理汇总了C#中System.Threading.ReaderWriterLockSlim.UsingReadLock方法的典型用法代码示例。如果您正苦于以下问题:C# ReaderWriterLockSlim.UsingReadLock方法的具体用法?C# ReaderWriterLockSlim.UsingReadLock怎么用?C# ReaderWriterLockSlim.UsingReadLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.ReaderWriterLockSlim的用法示例。


在下文中一共展示了ReaderWriterLockSlim.UsingReadLock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestReadWriteLockSlim

        public void TestReadWriteLockSlim()
        {
            var sentry = new ReaderWriterLockSlim();
              var sw = new Stopwatch();
              sw.Start();

              var barrier = new CountdownEvent(4);

              //first read
              ThreadPool.QueueUserWorkItem(_ =>
              {
            using (sentry.UsingReadLock())
            {
              Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds,
            Thread.CurrentThread.ManagedThreadId);

              Thread.Sleep(500);

              Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds,
            Thread.CurrentThread.ManagedThreadId);
            }
            barrier.Signal();
              });

              //write
              ThreadPool.QueueUserWorkItem(_ =>
              {
            Thread.Sleep(100);

            using (sentry.UsingWriteLock())
            {
              Console.WriteLine("Acquired write: {0}ms, thread={1}", sw.ElapsedMilliseconds,
            Thread.CurrentThread.ManagedThreadId);

              Thread.Sleep(500);
              Console.WriteLine("Releasing write: {0}ms, thread={1}", sw.ElapsedMilliseconds,
            Thread.CurrentThread.ManagedThreadId);
            }
            barrier.Signal();
              });

              //read that starts after write but executes before
              ThreadPool.QueueUserWorkItem(_ =>
              {
            Thread.Sleep(200);

            using (sentry.UsingReadLock())
            {

              Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds,
            Thread.CurrentThread.ManagedThreadId);

              Thread.Sleep(500);

              Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds,
            Thread.CurrentThread.ManagedThreadId);
            }
            barrier.Signal();
              });

            //this read task must wait for write on
              ThreadPool.QueueUserWorkItem(_ =>
              {
            Thread.Sleep(1000);

            using (sentry.UsingReadLock())
            {
              Console.WriteLine("Acquired read: {0}ms, thread={1}", sw.ElapsedMilliseconds,
            Thread.CurrentThread.ManagedThreadId);
              Thread.Sleep(500);

              Console.WriteLine("Releasing read: {0}ms, thread={1}", sw.ElapsedMilliseconds,
            Thread.CurrentThread.ManagedThreadId);
            }
            barrier.Signal();
              });

              barrier.Wait();
              sw.Stop();
              Console.WriteLine("Test finished");
        }
开发者ID:yuske,项目名称:ParallelDemo,代码行数:81,代码来源:TestRWLock.cs


注:本文中的System.Threading.ReaderWriterLockSlim.UsingReadLock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。