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


C# ReaderWriterLock.AcquireReaderLock方法代码示例

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


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

示例1: LockOnReaderWriterLock

        private static void LockOnReaderWriterLock()
        {
            Console.WriteLine("About to lock on the ReaderWriterLock. Debug after seeing \"Signaled to acquire the reader lock.\"");
            ReaderWriterLock rwLock = new ReaderWriterLock();

            ManualResetEvent rEvent = new ManualResetEvent(false);
            ManualResetEvent pEvent = new ManualResetEvent(false);

            ThreadPool.QueueUserWorkItem((object state) =>
            {
                rwLock.AcquireWriterLock(-1);
                Console.WriteLine("Writer lock acquired!");
                rEvent.Set();
                pEvent.WaitOne();
            });

            rEvent.WaitOne();

            Console.WriteLine("Signaled to acquire the reader lock.");

            rwLock.AcquireReaderLock(-1);

            Console.WriteLine("Reader lock acquired");

            pEvent.Set();

            Console.WriteLine("About to end the program. Press any key to exit.");
            Console.ReadKey();
        }
开发者ID:alienwaredream,项目名称:toolsdotnet,代码行数:29,代码来源:Program.cs

示例2: ReaderLock

        public ReaderLock(ReaderWriterLock rwLock)
        {
            if (null == rwLock) throw new Exception("Don't pass a null ReaderWriterLock object!");

            _lock = rwLock;
            _lock.AcquireReaderLock(Timeout.Infinite);
        }
开发者ID:anxkha,项目名称:DRM,代码行数:7,代码来源:ReaderLock.cs

示例3: ExecuteJob

 /// <summary>
 /// Executes a job on demand, rather than waiting for its regularly scheduled time.
 /// </summary>
 /// <param name="job">The job to be executed.</param>
 public static void ExecuteJob(JobBase job)
 {
     ReaderWriterLock rwLock = new ReaderWriterLock();
     try
     {
         rwLock.AcquireReaderLock(Timeout.Infinite);
         if (job.Executing == false)
         {
             LockCookie lockCookie = rwLock.UpgradeToWriterLock(Timeout.Infinite);
             try
             {
                 if (job.Executing == false)
                 {
                     job.Executing = true;
                     QueueJob(job);
                 }
             }
             finally
             {
                 rwLock.DowngradeFromWriterLock(ref lockCookie);
             }
         }
     }
     finally
     {
         rwLock.ReleaseReaderLock();
     }
 }
开发者ID:SolidSnake74,项目名称:SharpCore,代码行数:32,代码来源:Scheduler.cs

示例4: TestIsReaderLockHeld

		public void TestIsReaderLockHeld ()
		{
			rwlock = new ReaderWriterLock ();
			Assert.IsTrue (!rwlock.IsReaderLockHeld, "#1");
			rwlock.AcquireReaderLock (500);
			Assert.IsTrue (rwlock.IsReaderLockHeld, "#1");
			RunThread (new ThreadStart (IsReaderLockHeld_2));
			rwlock.ReleaseReaderLock ();
		}
开发者ID:nlhepler,项目名称:mono,代码行数:9,代码来源:ReaderWriterLockTest.cs

示例5: TestIsReaderLockHeld

		public void TestIsReaderLockHeld ()
		{
			rwlock = new ReaderWriterLock ();
			Assert ("a1", !rwlock.IsReaderLockHeld);
			rwlock.AcquireReaderLock (500);
			Assert ("a2", rwlock.IsReaderLockHeld);
			RunThread (new ThreadStart (IsReaderLockHeld_2));
			rwlock.ReleaseReaderLock ();
		}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:9,代码来源:ReaderWriterLockTest.cs

示例6: LockingObject

        public LockingObject(ReaderWriterLock _lock, AccessMode _lockMode)
        {
            readerWriterLock = _lock;
            accessMode = _lockMode;

            if (accessMode == AccessMode.Read)
            {
                readerWriterLock.AcquireReaderLock(-1);
            }
            else
            {
                readerWriterLock.AcquireWriterLock(-1);
            }
        }
开发者ID:wxgb9801,项目名称:Measurer,代码行数:14,代码来源:LockingObject.cs

示例7: ReadLock

        private ReadLock( ReaderWriterLock syncLock, TimeSpan timeout )
        {
            if( syncLock == null )
                throw new ArgumentNullException( "ReaderWriterLock syncLock" );

            _rwLock = syncLock;

            try
            {
                syncLock.AcquireReaderLock( timeout );
            }
            catch( ApplicationException )
            {
                _timedOut = true;
            }
        }
开发者ID:onesimoh,项目名称:Andamio,代码行数:16,代码来源:ReadLock.cs

示例8: ReaderAutoLocker

        /// <summary>
        /// Obtain read access for the given object.
        /// Throws ApplicationException if access has not been acquired within 30 secs.
        /// </summary>
        /// <param name="locker">Locker to acquire rights.</param>
        public ReaderAutoLocker(ReaderWriterLock locker)
        {
            this.wasFreed = false;
            this.locker = locker;
            this.wasHeld = true;

            if (this.locker.IsReaderLockHeld || this.locker.IsWriterLockHeld)
            {
                this.wasHeld = false;
                return ;
            }

            locker.AcquireReaderLock(30000);
            if (! locker.IsReaderLockHeld)
                throw new ApplicationException("Can't acquire reader rights. CPU is overloaded or deadlock.");
        }
开发者ID:ArsenShnurkov,项目名称:GenuineChannels,代码行数:21,代码来源:ReaderAutoLocker.cs

示例9: IICLockRegion

        public IICLockRegion(ReaderWriterLock innerLock, IICLockMode mode, int millisecondTimeout)
        {
            _innerLock = innerLock;
            _lockMode = mode;
            _msTimeout = millisecondTimeout;

            switch (mode) {
                case IICLockMode.ReaderLock:
                    _innerLock.AcquireReaderLock(millisecondTimeout);
                    break;
                case IICLockMode.WriterLock:
                    _innerLock.AcquireWriterLock(millisecondTimeout);
                    break;
                default:
                    throw new NotSupportedException("Unexcepted LockMode: " + mode);
            }
        }
开发者ID:amwtke,项目名称:commonlibrary,代码行数:17,代码来源:IICLockRegion.cs

示例10: ReaderWriter_When2CycleDetected_DoesNotThrows

        public void ReaderWriter_When2CycleDetected_DoesNotThrows()
        {
            ReaderWriterLock rwl = new ReaderWriterLock();
            Barrier barrier = new Barrier(2);

            Action t1 = () =>
                            {
                                rwl.AcquireReaderLock(Timeout.Infinite);
                                barrier.SignalAndWait();
                                rwl.UpgradeToWriterLock(Timeout.Infinite);
                                rwl.ReleaseWriterLock();
                            };

            Action t2 = () =>
                            {
                                rwl.AcquireReaderLock(Timeout.Infinite);
                                barrier.SignalAndWait();
                                Thread.Sleep(500);
                                rwl.ReleaseReaderLock();
                            };

            TestHelpers.InvokeSimultaneouslyAndWait(t1, t2);
        }
开发者ID:olachan,项目名称:PostSharp-Toolkits,代码行数:23,代码来源:DetectDeadlocksTests.cs

示例11: TestAcquireLocks

		public void TestAcquireLocks ()
		{
			rwlock = new ReaderWriterLock ();
			rwlock.AcquireReaderLock (500);
			rwlock.AcquireReaderLock (500);
			rwlock.ReleaseReaderLock ();
				Assert.IsTrue (rwlock.IsReaderLockHeld, "#1");			
			RunThread (new ThreadStart (AcquireLock_readerWorks));
			Assert.IsTrue (rwlock.IsReaderLockHeld);
			
			RunThread (new ThreadStart (AcquireLock_writerFails));
			rwlock.ReleaseReaderLock ();
			Assert.IsTrue (!rwlock.IsReaderLockHeld);
			
			RunThread (new ThreadStart (AcquireLock_writerWorks));
			
			rwlock.AcquireWriterLock (200);
			RunThread (new ThreadStart (AcquireLock_writerFails));
			RunThread (new ThreadStart (AcquireLock_readerFails));
			rwlock.ReleaseWriterLock ();
		}
开发者ID:nlhepler,项目名称:mono,代码行数:21,代码来源:ReaderWriterLockTest.cs

示例12: TestBug_55909

		public void TestBug_55909 ()
		{
			rwlock = new ReaderWriterLock ();
			ThreadRunner tr = StartThread (new ThreadStart(Bug_55909_Thread2));
			Thread.Sleep (200);
			rwlock.AcquireReaderLock (Timeout.Infinite);
			try {
				LockCookie lc = rwlock.UpgradeToWriterLock (Timeout.Infinite);
				Thread.Sleep (500);
			}
			finally { rwlock.ReleaseReaderLock(); }
			
			tr.Join ();
		}
开发者ID:nlhepler,项目名称:mono,代码行数:14,代码来源:ReaderWriterLockTest.cs

示例13: DowngradeTest

		public void DowngradeTest ()
		{
			LockCookie lc1, lc2, lc3, lc4;

			rwlock = new ReaderWriterLock ();

			rwlock.AcquireReaderLock (Timeout.Infinite);
			lc1 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
			rwlock.AcquireReaderLock (Timeout.Infinite);
			lc2 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
			rwlock.AcquireReaderLock (Timeout.Infinite);
			lc3 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
			rwlock.AcquireReaderLock (Timeout.Infinite);
			lc4 = rwlock.UpgradeToWriterLock (Timeout.Infinite);

			rwlock.DowngradeFromWriterLock (ref lc2);

			Assert.IsFalse (rwlock.IsReaderLockHeld, "A1");
			Assert.IsTrue (rwlock.IsWriterLockHeld, "A2");

			rwlock.ReleaseReaderLock ();

			Assert.IsFalse (rwlock.IsReaderLockHeld, "B1");
			Assert.IsTrue (rwlock.IsWriterLockHeld, "B2");

			rwlock.DowngradeFromWriterLock (ref lc4);

			Assert.IsFalse (rwlock.IsReaderLockHeld, "C1");
			Assert.IsTrue (rwlock.IsWriterLockHeld, "C2");

			rwlock.ReleaseReaderLock ();

			Assert.IsFalse (rwlock.IsReaderLockHeld, "D1");
			Assert.IsTrue (rwlock.IsWriterLockHeld, "D2");

			rwlock.DowngradeFromWriterLock (ref lc3);

			Assert.IsFalse (rwlock.IsReaderLockHeld, "E1");
			Assert.IsTrue (rwlock.IsWriterLockHeld, "E2");

			rwlock.ReleaseReaderLock ();

			Assert.IsFalse (rwlock.IsReaderLockHeld, "F1");
			Assert.IsTrue (rwlock.IsWriterLockHeld, "F2");

			rwlock.DowngradeFromWriterLock (ref lc1);

			Assert.IsTrue (rwlock.IsReaderLockHeld, "G1");
			Assert.IsFalse (rwlock.IsWriterLockHeld, "G2");

			rwlock.ReleaseReaderLock ();

			Assert.IsFalse (rwlock.IsReaderLockHeld, "H1");
			Assert.IsFalse (rwlock.IsWriterLockHeld, "H2");
		}
开发者ID:nlhepler,项目名称:mono,代码行数:55,代码来源:ReaderWriterLockTest.cs

示例14: TestReaderInsideWriter

		public void TestReaderInsideWriter ()
		{
			// Reader acquires and releases work like the writer equivalent
			
			rwlock = new ReaderWriterLock ();
			rwlock.AcquireWriterLock (-1);
			rwlock.AcquireReaderLock (-1);
			Assert ("u1", !rwlock.IsReaderLockHeld);
			Assert ("u2", rwlock.IsWriterLockHeld);
			rwlock.AcquireReaderLock (-1);
			Assert ("u3", !rwlock.IsReaderLockHeld);
			Assert ("u4", rwlock.IsWriterLockHeld);
			rwlock.ReleaseWriterLock ();
			Assert ("u5", !rwlock.IsReaderLockHeld);
			Assert ("u6", rwlock.IsWriterLockHeld);
			rwlock.ReleaseReaderLock ();
			Assert ("u7", !rwlock.IsReaderLockHeld);
			Assert ("u8", rwlock.IsWriterLockHeld);
			rwlock.ReleaseReaderLock ();
			Assert ("u9", !rwlock.IsReaderLockHeld);
			Assert ("u10", !rwlock.IsWriterLockHeld);
		}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:22,代码来源:ReaderWriterLockTest.cs

示例15: ReadLocker

 public ReadLocker(ref ReaderWriterLock rwl)
 {
     mRwl = rwl;
     mRwl.AcquireReaderLock(Constants.kLockTimoutMs);
 }
开发者ID:Amartel1986,项目名称:Deltix-Test2,代码行数:5,代码来源:RwlHelper.cs


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