本文整理汇总了C#中System.Threading.ReaderWriterLockSlim.ExitUpgradeableReadLock方法的典型用法代码示例。如果您正苦于以下问题:C# ReaderWriterLockSlim.ExitUpgradeableReadLock方法的具体用法?C# ReaderWriterLockSlim.ExitUpgradeableReadLock怎么用?C# ReaderWriterLockSlim.ExitUpgradeableReadLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.ReaderWriterLockSlim
的用法示例。
在下文中一共展示了ReaderWriterLockSlim.ExitUpgradeableReadLock方法的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();
}
});
}
示例2: 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();
}
}
示例3: RecursiveWriteUpgradeTest
public void RecursiveWriteUpgradeTest()
{
ReaderWriterLockSlim rwlock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
rwlock.EnterWriteLock();
Assert.IsTrue(rwlock.IsWriteLockHeld);
rwlock.EnterUpgradeableReadLock();
Assert.IsTrue(rwlock.IsUpgradeableReadLockHeld);
rwlock.ExitUpgradeableReadLock();
Assert.IsFalse(rwlock.IsUpgradeableReadLockHeld);
Assert.IsTrue(rwlock.IsWriteLockHeld);
rwlock.ExitWriteLock();
Assert.IsFalse(rwlock.IsWriteLockHeld);
rwlock.EnterWriteLock();
Assert.IsTrue(rwlock.IsWriteLockHeld);
}
示例4: RecursiveEnterExitUpgradableTest
public void RecursiveEnterExitUpgradableTest()
{
var v = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
v.EnterUpgradeableReadLock();
v.EnterUpgradeableReadLock();
v.EnterUpgradeableReadLock();
Assert.IsTrue(v.IsUpgradeableReadLockHeld);
Assert.AreEqual(3, v.RecursiveUpgradeCount);
v.ExitUpgradeableReadLock();
Assert.IsTrue(v.IsUpgradeableReadLockHeld);
Assert.AreEqual(2, v.RecursiveUpgradeCount);
}
示例5: 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
}
}
示例6: 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();
}
}
示例7: 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();
}
}
}
示例8: LoadSingleFactoryData
//- $LoadSingleFactoryData -//
private static void LoadSingleFactoryData(WebDomainData data, String factoryType, Object[] parameterArray, Map parameterMap, String source)
{
var readerWriterLockSlim = new ReaderWriterLockSlim();
try
{
IFactory factory = null;
//+
readerWriterLockSlim.EnterUpgradeableReadLock();
if (!RouteCache.HandlerFactoryCache.ContainsKey(factoryType) && !RouteCache.ProcessorFactoryCache.ContainsKey(factoryType))
{
readerWriterLockSlim.EnterWriteLock();
//+
try
{
if (!RouteCache.HandlerFactoryCache.ContainsKey(factoryType) && !RouteCache.ProcessorFactoryCache.ContainsKey(factoryType))
{
factory = ObjectCreator.CreateAs<IFactory>(factoryType);
if (factory == null)
{
throw new InvalidFactoryException(String.Format(Resource.Factory_Invalid, factoryType));
}
//+
FactoryData factoryData = FactoryData.Create(factoryType, parameterArray, parameterMap);
factoryData.Source = String.IsNullOrEmpty(source) ? Info.System : source;
if (factory is HandlerFactory)
{
data.HandlerFactoryDataList.Add(factoryData);
RouteCache.HandlerFactoryCache.Add(factoryType, factory);
}
else if (factory is ProcessorFactory)
{
data.ProcessorFactoryDataList.Add(factoryData);
RouteCache.ProcessorFactoryCache.Add(factoryType, factory);
}
}
}
finally
{
readerWriterLockSlim.ExitWriteLock();
}
}
}
catch (Exception ex)
{
if (WebProcessingReportController.Reporter.Initialized)
{
var map = new Map();
map.Add("Section", "Factory");
map.Add("Type", factoryType);
map.Add("Message", ex.Message);
map.Add("Exception Type", ex.GetType().FullName);
//+
WebProcessingReportController.Reporter.AddMap(map);
}
}
finally
{
readerWriterLockSlim.ExitUpgradeableReadLock();
}
}
示例9: EnterWriteLockWhileInUpgradeAndOtherWaiting
public void EnterWriteLockWhileInUpgradeAndOtherWaiting ()
{
var v = new ReaderWriterLockSlim ();
var task2 = new Task(() => {
v.EnterWriteLock();
v.ExitWriteLock();
});
var task1 = new Task(() =>
{
v.EnterUpgradeableReadLock ();
task2.Start ();
Thread.Sleep (100);
v.EnterWriteLock ();
v.ExitWriteLock ();
v.ExitUpgradeableReadLock ();
});
task1.Start ();
Assert.IsTrue (task1.Wait (500));
}
示例10: ReleaseReadLock
public static void ReleaseReadLock(ReaderWriterLockSlim locks)
{
if (locks.IsUpgradeableReadLockHeld)
locks.ExitUpgradeableReadLock();
}
示例11: ExitWriteLock
private void ExitWriteLock(ReaderWriterLockSlim detectorLock)
{
detectorLock.ExitWriteLock();
detectorLock.EnterReadLock();
detectorLock.ExitUpgradeableReadLock();
detectorLock.ExitReadLock();
}
示例12: 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
}
示例13: LoadSingleProcessorData
//.........这里部分代码省略.........
//+
try
{
if (!RouteCache.ProcessorCache.ContainsKey(processorType))
{
processor = ProcessorActivator.Create<IProcessor>(processorType, RouteCache.ProcessorFactoryCache);
if (processor == null)
{
throw new InvalidProcessorException(String.Format(Resource.Processor_Invalid, processorType));
}
//+
RouteCache.ProcessorCache.Add(processorType, processor);
}
}
finally
{
readerWriterLockSlim.ExitWriteLock();
}
}
if (processor == null)
{
return;
}
ProcessorData processorData;
if (processor is InitProcessor)
{
processorData = new ProcessorData
{
ProcessorType = processorType, ParameterArray = parameterArray
};
processorData.Source = String.IsNullOrEmpty(source) ? Info.System : source;
data.InitProcessorDataList.Add(processorData);
}
else if (processor is SelectionProcessor)
{
processorData = new ProcessorData
{
ProcessorType = processorType, ParameterArray = parameterArray
};
processorData.Source = String.IsNullOrEmpty(source) ? Info.System : source;
data.SelectionProcessorDataList.Add(processorData);
}
else if (processor is OverrideProcessor)
{
processorData = new ProcessorData
{
ProcessorType = processorType, ParameterArray = parameterArray
};
processorData.Source = String.IsNullOrEmpty(source) ? Info.System : source;
data.OverrideProcessorDataList.Add(processorData);
}
else if (processor is StateProcessor)
{
processorData = new ProcessorData
{
ProcessorType = processorType, ParameterArray = parameterArray
};
processorData.Source = String.IsNullOrEmpty(source) ? Info.System : source;
data.StateProcessorDataList.Add(processorData);
}
else if (processor is PostRenderProcessor)
{
processorData = new ProcessorData
{
ProcessorType = processorType, ParameterArray = parameterArray
};
processorData.Source = String.IsNullOrEmpty(source) ? Info.System : source;
data.PostRenderProcessorDataList.Add(processorData);
}
else if (processor is ErrorProcessor)
{
processorData = new ErrorProcessorData
{
ProcessorType = processorType, ParameterArray = parameterArray
};
processorData.Source = String.IsNullOrEmpty(source) ? Info.System : source;
var epd = ((ErrorProcessorData)processorData);
epd.Init();
data.ErrorProcessorDataList.Add(epd);
}
}
finally
{
readerWriterLockSlim.ExitUpgradeableReadLock();
}
}
catch (Exception ex)
{
if (WebProcessingReportController.Reporter.Initialized)
{
var map = new Map();
map.Add("Section", "Processor");
map.Add("Type", processorType);
map.Add("Message", ex.Message);
map.Add("Exception Type", ex.GetType().FullName);
//+
WebProcessingReportController.Reporter.AddMap(map);
}
}
}
示例14: 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 ();
}
示例15: DeadlockAvoidance
public static void DeadlockAvoidance()
{
using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim())
{
rwls.EnterReadLock();
Assert.Throws<LockRecursionException>(() => rwls.EnterReadLock());
Assert.Throws<LockRecursionException>(() => rwls.EnterUpgradeableReadLock());
Assert.Throws<LockRecursionException>(() => rwls.EnterWriteLock());
rwls.ExitReadLock();
rwls.EnterUpgradeableReadLock();
rwls.EnterReadLock();
Assert.Throws<LockRecursionException>(() => rwls.EnterReadLock());
rwls.ExitReadLock();
Assert.Throws<LockRecursionException>(() => rwls.EnterUpgradeableReadLock());
rwls.EnterWriteLock();
Assert.Throws<LockRecursionException>(() => rwls.EnterWriteLock());
rwls.ExitWriteLock();
rwls.ExitUpgradeableReadLock();
rwls.EnterWriteLock();
Assert.Throws<LockRecursionException>(() => rwls.EnterReadLock());
Assert.Throws<LockRecursionException>(() => rwls.EnterUpgradeableReadLock());
Assert.Throws<LockRecursionException>(() => rwls.EnterWriteLock());
rwls.ExitWriteLock();
}
using (ReaderWriterLockSlim rwls = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion))
{
rwls.EnterReadLock();
Assert.Throws<LockRecursionException>(() => rwls.EnterWriteLock());
rwls.EnterReadLock();
Assert.Throws<LockRecursionException>(() => rwls.EnterUpgradeableReadLock());
rwls.ExitReadLock();
rwls.ExitReadLock();
rwls.EnterUpgradeableReadLock();
rwls.EnterReadLock();
rwls.EnterUpgradeableReadLock();
rwls.ExitUpgradeableReadLock();
rwls.EnterReadLock();
rwls.ExitReadLock();
rwls.ExitReadLock();
rwls.EnterWriteLock();
rwls.EnterWriteLock();
rwls.ExitWriteLock();
rwls.ExitWriteLock();
rwls.ExitUpgradeableReadLock();
rwls.EnterWriteLock();
rwls.EnterReadLock();
rwls.ExitReadLock();
rwls.EnterUpgradeableReadLock();
rwls.ExitUpgradeableReadLock();
rwls.EnterWriteLock();
rwls.ExitWriteLock();
rwls.ExitWriteLock();
}
}