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


C# ReaderWriterLockSlim.EnterUpgradeableReadLock方法代码示例

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


在下文中一共展示了ReaderWriterLockSlim.EnterUpgradeableReadLock方法的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: PacketReceiver

        internal PacketReceiver(string LocalAddress) : base (LocalAddress)
        {
            packetReceiver = new Queue();
            tokenSource = new CancellationTokenSource();
            readerWriterLock = new ReaderWriterLockSlim();

            tokenSource.Token.Register(() => { 
                // Clear on cancel
                packetReceiver.Clear(); 
            });


            var thReceiveQueue = Task.Factory.StartNew(() => {
                while (tokenSource.Token.IsCancellationRequested == false)
                {
                    readerWriterLock.EnterUpgradeableReadLock();
                    if (packetReceiver.Count > 0)
                    {
                        readerWriterLock.EnterWriteLock();

                        byte[] data = (byte[])packetReceiver.Dequeue();

                        readerWriterLock.ExitWriteLock();

                        if (OnNewPacketReceived != null)
                            OnNewPacketReceived(this, new NewPacketEventArgs(data));
                    }

                    readerWriterLock.ExitUpgradeableReadLock();
                }
            });

        }
开发者ID:jasper22,项目名称:UbuntuUsbMonitor,代码行数:33,代码来源:PacketReceiver.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: 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

示例5: 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

示例6: NestableUpgradableReadLock

        /// <summary>
        /// Initializes a new instance of the <see cref="ReadLock"/> class.
        /// </summary>
        /// <param name="readerWriterLockSlim">The reader writer lock slim.</param>
        public NestableUpgradableReadLock(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.IsUpgradeableReadLockHeld
                || readerWriterLockSlim.IsWriteLockHeld)
            {
                lockAcquired = false;
            }
            else
            {
                readerWriterLockSlim.EnterUpgradeableReadLock();
                lockAcquired = true;
            }
        }
开发者ID:dmoonfire,项目名称:mfgames-cil,代码行数:21,代码来源:NestableUpgradableReadLock.cs

示例7: SlimUpgradeableReadLockHolder

        public SlimUpgradeableReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock, bool wasLockAlreadyHelf)
        {
            this.locker = locker;
            if (wasLockAlreadyHelf)
            {
                lockAcquired = true;
                wasLockAlreadyHeld = true;
                return;
            }

            if(waitForLock)
            {
                locker.EnterUpgradeableReadLock();
                lockAcquired = true;
                return;
            }

            lockAcquired = locker.TryEnterUpgradeableReadLock(0);
        }
开发者ID:brianmatic,项目名称:n2cms,代码行数:19,代码来源:SlimUpgradeableReadLockHolder.cs

示例8: 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

示例9: DontReleaseWaitingReadersWhenThereAreWaitingWriters

        public static void DontReleaseWaitingReadersWhenThereAreWaitingWriters()
        {
            using(var rwls = new ReaderWriterLockSlim())
            {
                rwls.EnterUpgradeableReadLock();
                rwls.EnterWriteLock();
                // Typical order of execution: 0

                // Add a waiting writer
                var threads = new Thread[2];
                using(var beforeEnterWriteLock = new ManualResetEvent(false))
                {
                    var thread =
                        new Thread(() =>
                        {
                            beforeEnterWriteLock.Set();
                            rwls.EnterWriteLock();
                            // Typical order of execution: 3
                            rwls.ExitWriteLock();
                        });
                    thread.IsBackground = true;
                    thread.Start();
                    threads[0] = thread;
                    beforeEnterWriteLock.WaitOne();
                }

                // Add a waiting reader
                using(var beforeEnterReadLock = new ManualResetEvent(false))
                {
                    var thread =
                        new Thread(() =>
                        {
                            beforeEnterReadLock.Set();
                            rwls.EnterReadLock();
                            // Typical order of execution: 4
                            rwls.ExitReadLock();
                        });
                    thread.IsBackground = true;
                    thread.Start();
                    threads[1] = thread;
                    beforeEnterReadLock.WaitOne();
                }

                // Wait for the background threads to block waiting for their locks
                Thread.Sleep(1000);

                // Typical order of execution: 1
                rwls.ExitWriteLock();
                // At this point there is still one reader and one waiting writer, so the reader-writer lock should not try to
                // release any of the threads waiting for a lock

                // Typical order of execution: 2
                rwls.ExitUpgradeableReadLock();
                // At this point, the waiting writer should be released, and the waiting reader should not

                foreach(var thread in threads)
                    thread.Join();
                // Typical order of execution: 5
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:60,代码来源:ReaderWriterLockSlimTests.cs

示例10: InvalidExits

        public static void InvalidExits(LockRecursionPolicy policy)
        {
            using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim(policy))
            {
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitReadLock());
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitUpgradeableReadLock());
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitWriteLock());

                rwls.EnterReadLock();
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitUpgradeableReadLock());
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitWriteLock());
                rwls.ExitReadLock();

                rwls.EnterUpgradeableReadLock();
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitReadLock());
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitWriteLock());
                rwls.ExitUpgradeableReadLock();

                rwls.EnterWriteLock();
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitReadLock());
                Assert.Throws<SynchronizationLockException>(() => rwls.ExitUpgradeableReadLock());
                rwls.ExitWriteLock();

                using (Barrier barrier = new Barrier(2))
                {
                    Task t = Task.Factory.StartNew(() =>
                    {
                        rwls.EnterWriteLock();
                        barrier.SignalAndWait();
                        barrier.SignalAndWait();
                        rwls.ExitWriteLock();
                    }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);

                    barrier.SignalAndWait();
                    Assert.Throws<SynchronizationLockException>(() => rwls.ExitWriteLock());
                    barrier.SignalAndWait();

                    t.GetAwaiter().GetResult();
                }
            }
        }
开发者ID:ESgarbi,项目名称:corefx,代码行数:41,代码来源:ReaderWriterLockSlimTests.cs

示例11: WriterToUpgradeableReaderChain

 public static void WriterToUpgradeableReaderChain()
 {
     using (AutoResetEvent are = new AutoResetEvent(false))
     using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim())
     {
         rwls.EnterWriteLock();
         Task t = Task.Factory.StartNew(() =>
         {
             Assert.False(rwls.TryEnterUpgradeableReadLock(TimeSpan.FromMilliseconds(10)));
             Task.Run(() => are.Set()); // ideally this won't fire until we've called EnterReadLock, but it's a benign race in that the test will succeed either way
             rwls.EnterUpgradeableReadLock();
             rwls.ExitUpgradeableReadLock();
         }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
         are.WaitOne();
         rwls.ExitWriteLock();
         t.GetAwaiter().GetResult();
     }
 }
开发者ID:ESgarbi,项目名称:corefx,代码行数:18,代码来源:ReaderWriterLockSlimTests.cs

示例12: RecursiveReadPlusUpgradeableLockTest

		public void RecursiveReadPlusUpgradeableLockTest ()
		{
			var v = new ReaderWriterLockSlim (LockRecursionPolicy.SupportsRecursion);

			try {
				v.EnterReadLock ();
				v.EnterUpgradeableReadLock ();
				Assert.Fail ("1");
			} catch (LockRecursionException ex) {
				Assert.IsNotNull (ex, "#1");
			}
		}
开发者ID:cthrax,项目名称:mono,代码行数:12,代码来源:ReaderWriterLockSlimTest.cs

示例13: Foo3

        private void Foo3()
        {
            var concurentDictionary = new Dictionary<int, int>();
            var rwLockSlim = new ReaderWriterLockSlim();

            var w = new ManualResetEvent(false);
            int timedCalled = 0;
            var threads = new List<Thread>();
            int j;
            Lazy<int> lazy = new Lazy<int>(() => { Interlocked.Increment(ref timedCalled); return 1; });
            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                threads.Add(new Thread(() =>
                {
                    w.WaitOne();
                    rwLockSlim.EnterUpgradeableReadLock();
                    try
                    {
                        if (!concurentDictionary.TryGetValue(1, out j))
                        {
                            rwLockSlim.EnterWriteLock();
                            try
                            {
                                Interlocked.Increment(ref timedCalled);
                                concurentDictionary[1] = 1;
                            }
                            finally
                            {
                                rwLockSlim.ExitWriteLock();
                            }
                        }
                    }
                    finally
                    {
                        rwLockSlim.ExitUpgradeableReadLock();

                    }
                }));

                threads.Last().Start();
            }

            w.Set();//release all threads to start at the same time
            Thread.Sleep(100);
            Console.WriteLine(timedCalled);// output is 1
        }
开发者ID:HK-Zhang,项目名称:Grains,代码行数:46,代码来源:ThreadSafeDicDemo.cs

示例14: Dispose_UpgradeableReadLock

 public void Dispose_UpgradeableReadLock()
 {
     var rwl = new ReaderWriterLockSlim();
     rwl.EnterUpgradeableReadLock();
     try
     {
         rwl.Dispose();
         Assert.Fail("1");
     }
     catch (SynchronizationLockException)
     {
     }
 }
开发者ID:mesheets,项目名称:Theraot-CF,代码行数:13,代码来源:ReaderWriterLockSlimTest.cs

示例15: EnterUpgradeableReadLock

		public void EnterUpgradeableReadLock ()
		{
			var v = new ReaderWriterLockSlim ();

			v.EnterUpgradeableReadLock ();
			Assert.IsTrue (v.IsUpgradeableReadLockHeld, "A");
			Assert.AreEqual (0, v.RecursiveWriteCount, "A1");
			Assert.AreEqual (0, v.RecursiveReadCount, "A2");
			Assert.AreEqual (1, v.RecursiveUpgradeCount, "A3");
			Assert.AreEqual (0, v.WaitingReadCount, "A4");
			Assert.AreEqual (0, v.WaitingUpgradeCount, "A5");
			Assert.AreEqual (0, v.WaitingWriteCount, "A6");
			v.ExitUpgradeableReadLock ();

			v.EnterUpgradeableReadLock ();
			Assert.IsTrue (v.IsUpgradeableReadLockHeld, "B");
			Assert.AreEqual (0, v.RecursiveWriteCount, "B1");
			Assert.AreEqual (0, v.RecursiveReadCount, "B2");
			Assert.AreEqual (1, v.RecursiveUpgradeCount, "B3");
			Assert.AreEqual (0, v.WaitingReadCount, "B4");
			Assert.AreEqual (0, v.WaitingUpgradeCount, "B5");
			Assert.AreEqual (0, v.WaitingWriteCount, "B6");

			v.EnterReadLock ();
			v.ExitUpgradeableReadLock ();

			Assert.IsTrue (v.IsReadLockHeld, "C");
			Assert.AreEqual (0, v.RecursiveWriteCount, "C1");
			Assert.AreEqual (1, v.RecursiveReadCount, "C2");
			Assert.AreEqual (0, v.RecursiveUpgradeCount, "C3");
			Assert.AreEqual (0, v.WaitingReadCount, "C4");
			Assert.AreEqual (0, v.WaitingUpgradeCount, "C5");
			Assert.AreEqual (0, v.WaitingWriteCount, "C6");

			v.ExitReadLock ();
		}
开发者ID:cthrax,项目名称:mono,代码行数:36,代码来源:ReaderWriterLockSlimTest.cs


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