本文整理汇总了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)
{
}
}
示例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();
}
});
}
示例3: ReadLock
public ReadLock(ReaderWriterLockSlim rwl, bool upgradeable = false)
{
this.rwl = rwl;
this.upgradeable = upgradeable;
if (upgradeable)
rwl.EnterUpgradeableReadLock();
else
rwl.EnterReadLock();
}
示例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();
}
}
示例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;
}
}
示例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;
}
}
示例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);
}
示例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());
}
}
示例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
}
}
示例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();
}
}
}
示例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();
}
}
示例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");
}
}
示例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
}
示例14: Dispose_UpgradeableReadLock
public void Dispose_UpgradeableReadLock()
{
var rwl = new ReaderWriterLockSlim();
rwl.EnterUpgradeableReadLock();
try
{
rwl.Dispose();
Assert.Fail("1");
}
catch (SynchronizationLockException)
{
}
}
示例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 ();
}