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


C# ReaderWriterLockSlim.EnterWriteLock方法代码示例

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


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

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

示例2: SlimWriteLockHolder

		public SlimWriteLockHolder(ReaderWriterLockSlim locker, bool waitForLock)
		{
			this.locker = locker;
			if(waitForLock)
			{
				locker.EnterWriteLock();
				lockAcquired = true;
				return;
			}
			lockAcquired = locker.TryEnterWriteLock(0);
		}
开发者ID:JulianBirch,项目名称:Castle.Core,代码行数:11,代码来源:SlimWriteLockHolder.cs

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

示例4: AtomWrite

 private static void AtomWrite(ReaderWriterLockSlim readerWriterLockSlim, Action action)
 {
     readerWriterLockSlim.EnterWriteLock();
     try
     {
         action();
     }
     finally
     {
         readerWriterLockSlim.ExitWriteLock();
     }
 }
开发者ID:kouweizhong,项目名称:equeue,代码行数:12,代码来源:ProcessQueue.cs

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

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

示例7: TestWindowViewModel

        public TestWindowViewModel()
        {
            RWLock = new ReaderWriterLockSlim();
            Observable = new ObservableCollection<int>();
            BindingOperations.EnableCollectionSynchronization(Observable, RWLock, new CollectionSynchronizationCallback(lockCollection));

            AddCommand = new Command(() =>
            {
                Task.Factory.StartNew(() =>
                {
                    RWLock.EnterWriteLock();
                    for (int i = 0; i < 10; i++)
                    {
                        Observable.Add(i);
                    }
                    RWLock.ExitWriteLock();
                });
            });

            ClearCommand = new Command(() =>
                {
                    Task.Factory.StartNew(() =>
                {
                    RWLock.EnterWriteLock();
                    Observable.Move(0,5);
                    RWLock.ExitWriteLock();

                    int k = 0;

                    for (int i = 0; i < 1000; i++)
                    {
                        k++;
                    }
                });
                });
        }
开发者ID:iejeecee,项目名称:mediaviewer,代码行数:36,代码来源:TestWindowViewModel.cs

示例8: NestableWriteLock

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

示例9: LockVSReaderWriterLockVSReaderWriterLockSlimVSHandle

		public void LockVSReaderWriterLockVSReaderWriterLockSlimVSHandle()
		{
			TestName("Lock vs. ReaderWriterLock vs. ReaderWriterLockSlim vs. Handle-based");

			Object o = new Object();
			
			CodeTimer.Time("Lock perf: Monitor", iterations,
			   () =>
			   {
				   Monitor.Enter(o);
				   //
				   Monitor.Exit(o);
			   });

			CodeTimer.Time("Lock perf: Lock", iterations,
			   () =>
			   {
				   lock (o)
				   {
				   }
				  
			   });

			ReaderWriterLockSlim rwls = new ReaderWriterLockSlim();
			CodeTimer.Time("Lock perf: ReaderWriterLockSlim", iterations,
			   () =>
			   {
				   rwls.EnterWriteLock();
				   rwls.ExitWriteLock();
			   });

			ReaderWriterLock rwl = new ReaderWriterLock();
			CodeTimer.Time("Lock perf: ReaderWriterLock", iterations,
			   () =>
			   {
				   rwl.AcquireWriterLock(Timeout.Infinite);
				   rwl.ReleaseWriterLock();
			   });

			Mutex mutex = new Mutex();
			CodeTimer.Time("Lock perf: Mutex", iterations,
			   () =>
			   {
				   mutex.WaitOne();
				   mutex.ReleaseMutex();
			   });
		}
开发者ID:denisefan28,项目名称:Wind,代码行数:47,代码来源:ThreadingPerformanceTest.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: 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

示例12: XXX

        public void XXX()
        {
            //var l = new ReaderWriterLockSlimEx();

            //using (l.AcquireReadLock())
            //{
            //    // access lock here so it's not optimized-away after release build
            //    Assert.IsNotNull(l);
            //}

            var l = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);

            l.EnterWriteLock();

            Assert.IsTrue(l.IsWriteLockHeld);

            Task.Factory.StartNew(() =>
            {
                Assert.IsFalse(l.IsWriteLockHeld);
            }).Wait();
            
        }
开发者ID:squaredinfinity,项目名称:Foundation,代码行数:22,代码来源:ReaderWriterLockSlimEx.tests.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: 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:cthrax,项目名称:mono,代码行数:23,代码来源:ReaderWriterLockSlimTest.cs

示例15: RecursiveReadPlusWriteLockTest

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

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


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