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


C# ReaderWriterLockSlim.EnterReadLock方法代码示例

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


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

示例1: Dispose_Errors

        public void Dispose_Errors()
        {
            var v = new ReaderWriterLockSlim();
            v.Dispose();

            try
            {
                v.EnterUpgradeableReadLock();
                Assert.Fail("1");
            }
            catch (ObjectDisposedException)
            {
            }

            try
            {
                v.EnterReadLock();
                Assert.Fail("2");
            }
            catch (ObjectDisposedException)
            {
            }

            try
            {
                v.EnterWriteLock();
                Assert.Fail("3");
            }
            catch (ObjectDisposedException)
            {
            }
        }
开发者ID:mesheets,项目名称:Theraot-CF,代码行数:32,代码来源:ReaderWriterLockSlimTest.cs

示例2: Dispose_WithReadLock

		public void Dispose_WithReadLock ()
		{
			var rwl = new ReaderWriterLockSlim ();
			rwl.EnterReadLock ();
			try {
				rwl.Dispose ();
				Assert.Fail ("1");
			} catch (SynchronizationLockException) {
			}
		}
开发者ID:Profit0004,项目名称:mono,代码行数:10,代码来源:ReaderWriterLockSlimTest.cs

示例3: ReadLock

        public ReadLock(ReaderWriterLockSlim rwl, bool upgradeable = false)
        {
            this.rwl = rwl;
            this.upgradeable = upgradeable;

            if (upgradeable)
                rwl.EnterUpgradeableReadLock();
            else
                rwl.EnterReadLock();
        }
开发者ID:InjectionDev,项目名称:Dev.D3,代码行数:10,代码来源:Locks.cs

示例4: SlimReadLockHolder

		public SlimReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock)
		{
			this.locker = locker;
			if(waitForLock)
			{
				locker.EnterReadLock();
				lockAcquired = true;
				return;
			}
			lockAcquired = locker.TryEnterReadLock(0);
		}
开发者ID:ArthurYiL,项目名称:JustMockLite,代码行数:11,代码来源:SlimReadLockHolder.cs

示例5: ReadWriteLock

        public ReadWriteLock(ReaderWriterLockSlim lockObject, bool forWrite)
        {
            if (lockObject == null)
                throw new ArgumentNullException("lockObject");
            this.lockObject = lockObject;
            this.forWrite = forWrite;

            if (forWrite)
                lockObject.EnterWriteLock();
            else
                lockObject.EnterReadLock();
        }
开发者ID:pieceofsummer,项目名称:TouchRemote,代码行数:12,代码来源:ReadWriteLock.cs

示例6: EnterExit

        public static void EnterExit()
        {
            using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim())
            {
                Assert.False(rwls.IsReadLockHeld);
                rwls.EnterReadLock();
                Assert.True(rwls.IsReadLockHeld);
                rwls.ExitReadLock();
                Assert.False(rwls.IsReadLockHeld);

                Assert.False(rwls.IsUpgradeableReadLockHeld);
                rwls.EnterUpgradeableReadLock();
                Assert.True(rwls.IsUpgradeableReadLockHeld);
                rwls.ExitUpgradeableReadLock();
                Assert.False(rwls.IsUpgradeableReadLockHeld);

                Assert.False(rwls.IsWriteLockHeld);
                rwls.EnterWriteLock();
                Assert.True(rwls.IsWriteLockHeld);
                rwls.ExitWriteLock();
                Assert.False(rwls.IsWriteLockHeld);

                Assert.False(rwls.IsUpgradeableReadLockHeld);
                rwls.EnterUpgradeableReadLock();
                Assert.False(rwls.IsWriteLockHeld);
                Assert.True(rwls.IsUpgradeableReadLockHeld);
                rwls.EnterWriteLock();
                Assert.True(rwls.IsWriteLockHeld);
                rwls.ExitWriteLock();
                Assert.False(rwls.IsWriteLockHeld);
                Assert.True(rwls.IsUpgradeableReadLockHeld);
                rwls.ExitUpgradeableReadLock();
                Assert.False(rwls.IsUpgradeableReadLockHeld);

                Assert.True(rwls.TryEnterReadLock(0));
                rwls.ExitReadLock();

                Assert.True(rwls.TryEnterReadLock(Timeout.InfiniteTimeSpan));
                rwls.ExitReadLock();

                Assert.True(rwls.TryEnterUpgradeableReadLock(0));
                rwls.ExitUpgradeableReadLock();

                Assert.True(rwls.TryEnterUpgradeableReadLock(Timeout.InfiniteTimeSpan));
                rwls.ExitUpgradeableReadLock();

                Assert.True(rwls.TryEnterWriteLock(0));
                rwls.ExitWriteLock();

                Assert.True(rwls.TryEnterWriteLock(Timeout.InfiniteTimeSpan));
                rwls.ExitWriteLock();
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:53,代码来源:ReaderWriterLockSlimTests.cs

示例7: n3_multithreading

		public void n3_multithreading()
		{
			int run = 1;
			int count = 1;
			const int max = 64;
			var exceptions = new List<Exception>();
			var locker = new ReaderWriterLockSlim();

			locker.EnterWriteLock();

			for (int i = 0; i < max; i++)
			{
				ThreadPool.QueueUserWorkItem((_) =>
				{
					Interlocked.Increment(ref count);
					locker.EnterReadLock();
					locker.ExitReadLock();

					try
					{
						while (Thread.VolatileRead(ref run) != 0)
						{
							DestroyReaders(
								CreateReaders(16), 0, false);
						}
					}
					catch (Exception ex)
					{
						exceptions.Add(ex);
					}

					Interlocked.Increment(ref count);
				});
			}

			while (Thread.VolatileRead(ref count) < max)
				Thread.Sleep(100);
			count = 1;
			locker.ExitWriteLock();


			Thread.Sleep(60000);
			run = 0;


			while (Thread.VolatileRead(ref count) < max)
				Thread.Sleep(1000);


			if (exceptions.Count > 0)
				throw exceptions[0];
		}
开发者ID:five-x,项目名称:siprevo,代码行数:52,代码来源:SipMessageReaderOptimized2Test.cs

示例8: OneManyLockIsFasterThanReaderWriterLockSlim

        public void OneManyLockIsFasterThanReaderWriterLockSlim()
        {
            // OneManyLock.
            var sw = Stopwatch.StartNew();
            using (var oml = new OneManyLock())
            {
                oml.Enter(true); 
                M(); 
                oml.Leave(true);
                for (Int32 i = 0; i < c_iterations; i++)
                {
                    oml.Enter(true); 
                    M();
                    oml.Leave(true);
                }
            }

            Int64 omlElapsedMilliseconds = sw.ElapsedMilliseconds;

            // ReaderWriterLockSlim.
            sw.Restart();
            using (var rwls = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion))
            {
                rwls.EnterReadLock();
                M();
                rwls.ExitReadLock();
                for (Int32 i = 0; i < c_iterations; i++)
                {
                    rwls.EnterReadLock();
                    M();
                    rwls.ExitReadLock();
                }
            }

            Int64 rwlsElapsedMilliseconds = sw.ElapsedMilliseconds;

            Assert.True(omlElapsedMilliseconds < rwlsElapsedMilliseconds);
        }
开发者ID:gnikolaropoulos,项目名称:.Net-Labs,代码行数:38,代码来源:OneManyLockTest.cs

示例9: TestMethod2

        public void TestMethod2()
        {
            ISomething something = new Something();
            var tasks = new List<Task>();
            var rwlock = new ReaderWriterLockSlim();

            for (var i = 0; i < 100; i++)
            {
                // These threads should never see "nothing"....
                tasks.Add(Task.Run(() =>
                {
                    for (var x = 0; x < 1000000; x++)
                    {
                        rwlock.EnterReadLock();
                        try
                        {
                            Thread.Sleep(100);
                            Assert.That(something.IsNothing, Is.False);
                        }
                        finally
                        {
                            rwlock.ExitReadLock();
                        }
                    }
                }));
            }

            // ... even though this one keeps setting it to nothing
            tasks.Add(Task.Run(() =>
            {
                for (var x = 0; x < 1000000; x++)
                {
                    rwlock.EnterWriteLock();
                    try
                    {
                        something = new Nothing();
                        Thread.Sleep(100);
                        something = new Something();
                    }
                    finally
                    {
                        rwlock.ExitWriteLock();
                    }
                }
            }));

            Task.WhenAll(tasks).Wait();
        }
开发者ID:searbe,项目名称:redis-client,代码行数:48,代码来源:UnitTest1.cs

示例10: LockExtend

 public LockExtend(ReaderWriterLockSlim slim, LockMode mode)
 {
     _Slim = slim;
     this._Mode = mode;
     switch(mode)
     {
         case LockMode.Write:
             slim.EnterWriteLock();
             break;
         case LockMode.Read:
             slim.EnterReadLock();
             break;
         case LockMode.UpgradeableRead:
             slim.EnterUpgradeableReadLock();
             break;
     }
 }
开发者ID:glorylee,项目名称:Aoite,代码行数:17,代码来源:LockSlim.cs

示例11: NestableReadLock

        /// <summary>
        /// Initializes a new instance of the <see cref="ReadLock"/> class.
        /// </summary>
        /// <param name="readerWriterLockSlim">The reader writer lock slim.</param>
        public NestableReadLock(ReaderWriterLockSlim readerWriterLockSlim)
        {
            // Keep track of the lock since we'll need it to release the lock.
            this.readerWriterLockSlim = readerWriterLockSlim;

            // If we already have a read or write lock, we don't do anything.
            if (readerWriterLockSlim.IsReadLockHeld
                || readerWriterLockSlim.IsUpgradeableReadLockHeld
                || readerWriterLockSlim.IsWriteLockHeld)
            {
                lockAcquired = false;
            }
            else
            {
                readerWriterLockSlim.EnterReadLock();
                lockAcquired = true;
            }
        }
开发者ID:dmoonfire,项目名称:mfgames-cil,代码行数:22,代码来源:NestableReadLock.cs

示例12: ScopedReaderWriterLock

        public ScopedReaderWriterLock(ReaderWriterLockSlim readerWriterLock, bool readOnly = true)
        {
            if (_readerWriterLock == null)
            {
                throw new ArgumentNullException(nameof(readerWriterLock));
            }

            _readerWriterLock = readerWriterLock;
            _readOnly = readOnly;

            if (_readOnly)
            {
                _readerWriterLock.EnterReadLock();
            }
            else
            {
                _readerWriterLock.EnterWriteLock();
            }
        }
开发者ID:ali-hk,项目名称:Toolkit,代码行数:19,代码来源:ScopedReaderWriterLock.cs

示例13: Dispose

        public static void Dispose()
        {
            ReaderWriterLockSlim rwls;

            rwls = new ReaderWriterLockSlim();
            rwls.Dispose();
            Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterReadLock(0));
            Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterUpgradeableReadLock(0));
            Assert.Throws<ObjectDisposedException>(() => rwls.TryEnterWriteLock(0));
            rwls.Dispose();

            for (int i = 0; i < 3; i++)
            {
                rwls = new ReaderWriterLockSlim();
                switch (i)
                {
                    case 0: rwls.EnterReadLock(); break;
                    case 1: rwls.EnterUpgradeableReadLock(); break;
                    case 2: rwls.EnterWriteLock(); break;
                }
                Assert.Throws<SynchronizationLockException>(() => rwls.Dispose());
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:23,代码来源:ReaderWriterLockSlimTests.cs

示例14: EnterReadLock_MultiRead

		public void EnterReadLock_MultiRead ()
		{
			var v = new ReaderWriterLockSlim ();
			int local = 10;

			var r = from i in Enumerable.Range (1, 30) select new Thread (() => {

				// Just to cause some contention
				Thread.Sleep (100);

				v.EnterReadLock ();

				Assert.AreEqual (10, local);
			});

			var threads = r.ToList ();

			foreach (var t in threads) {
				t.Start ();
			}

			foreach (var t in threads) {
				// Console.WriteLine (t.ThreadState);
				t.Join ();
			}
		}
开发者ID:cthrax,项目名称:mono,代码行数:26,代码来源:ReaderWriterLockSlimTest.cs

示例15: TryEnterWriteLock_WhileReading

		public void TryEnterWriteLock_WhileReading ()
		{
			var v = new ReaderWriterLockSlim ();
			AutoResetEvent ev = new AutoResetEvent (false);
			AutoResetEvent ev2 = new AutoResetEvent (false);

			Thread t1 = new Thread (() => {
				v.EnterReadLock ();
				ev2.Set ();
				ev.WaitOne ();
				v.ExitReadLock ();
			});

			t1.Start ();
			ev2.WaitOne ();

			Assert.IsFalse (v.TryEnterWriteLock (100));
			Assert.IsFalse (v.TryEnterReadLock (100));
			ev.Set ();

			Assert.IsTrue (v.TryEnterWriteLock (100));
		}
开发者ID:cthrax,项目名称:mono,代码行数:22,代码来源:ReaderWriterLockSlimTest.cs


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