本文整理汇总了C#中System.Threading.ReaderWriterLockSlim.ExitWriteLock方法的典型用法代码示例。如果您正苦于以下问题:C# ReaderWriterLockSlim.ExitWriteLock方法的具体用法?C# ReaderWriterLockSlim.ExitWriteLock怎么用?C# ReaderWriterLockSlim.ExitWriteLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.ReaderWriterLockSlim
的用法示例。
在下文中一共展示了ReaderWriterLockSlim.ExitWriteLock方法的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: EntersWriteLock
public void EntersWriteLock()
{
var slim = new ReaderWriterLockSlim();
var token = slim.Write();
Assert.IsTrue(slim.IsWriteLockHeld);
slim.ExitWriteLock();
slim.Dispose();
}
示例3: AtomWrite
private static void AtomWrite(ReaderWriterLockSlim readerWriterLockSlim, Action action)
{
readerWriterLockSlim.EnterWriteLock();
try
{
action();
}
finally
{
readerWriterLockSlim.ExitWriteLock();
}
}
示例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: 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];
}
示例6: 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++;
}
});
});
}
示例7: 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();
});
}
示例8: FreeWriterLock
private static void FreeWriterLock(ReaderWriterLockSlim oLock)
{
oLock.ExitWriteLock();
}
示例9: EnterWriteLock_MultiRead
public void EnterWriteLock_MultiRead()
{
var v = new ReaderWriterLockSlim();
int local = 10;
int ready_count = 0;
int entered_count = 0;
const int thread_count = 10;
var r = from i in Enumerable.Range(1, thread_count)
select new Thread(() =>
{
Interlocked.Increment(ref ready_count);
v.EnterReadLock();
Interlocked.Increment(ref entered_count);
Assert.AreEqual(11, local);
});
v.EnterWriteLock();
var threads = r.ToList();
foreach (var t in threads)
{
t.Start();
}
while (ready_count != thread_count)
Thread.Sleep(10);
/* Extra up to 2s of sleep to ensure all threads got the chance to enter the lock */
for (int i = 0; i < 200 && v.WaitingReadCount != thread_count; ++i)
Thread.Sleep(10);
local = 11;
Assert.AreEqual(0, v.WaitingWriteCount, "in waiting write");
Assert.AreEqual(thread_count, v.WaitingReadCount, "in waiting read");
Assert.AreEqual(0, v.WaitingUpgradeCount, "in waiting upgrade");
v.ExitWriteLock();
foreach (var t in threads)
{
// Console.WriteLine (t.ThreadState);
t.Join();
}
}
示例10: ExitWriteLock
private void ExitWriteLock(ReaderWriterLockSlim detectorLock)
{
detectorLock.ExitWriteLock();
detectorLock.EnterReadLock();
detectorLock.ExitUpgradeableReadLock();
detectorLock.ExitReadLock();
}
示例11: 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
}
}
示例12: EnterWriteLock
public void EnterWriteLock ()
{
var v = new ReaderWriterLockSlim ();
v.EnterWriteLock ();
Assert.IsTrue (v.IsWriteLockHeld, "A");
Assert.AreEqual (1, v.RecursiveWriteCount, "A1");
Assert.AreEqual (0, v.RecursiveReadCount, "A2");
Assert.AreEqual (0, v.RecursiveUpgradeCount, "A3");
Assert.AreEqual (0, v.WaitingReadCount, "A4");
Assert.AreEqual (0, v.WaitingUpgradeCount, "A5");
Assert.AreEqual (0, v.WaitingWriteCount, "A6");
v.ExitWriteLock ();
v.EnterWriteLock ();
Assert.IsTrue (v.IsWriteLockHeld, "B");
Assert.AreEqual (1, v.RecursiveWriteCount, "B1");
Assert.AreEqual (0, v.RecursiveReadCount, "B2");
Assert.AreEqual (0, v.RecursiveUpgradeCount, "B3");
Assert.AreEqual (0, v.WaitingReadCount, "B4");
Assert.AreEqual (0, v.WaitingUpgradeCount, "B5");
Assert.AreEqual (0, v.WaitingWriteCount, "B6");
v.ExitWriteLock ();
}
示例13: EnterWriteLock_After_ExitUpgradeableReadLock
public void EnterWriteLock_After_ExitUpgradeableReadLock ()
{
var v = new ReaderWriterLockSlim ();
v.EnterUpgradeableReadLock ();
Assert.IsTrue (v.TryEnterWriteLock (100));
v.ExitWriteLock ();
v.ExitUpgradeableReadLock ();
Assert.IsTrue (v.TryEnterWriteLock (100));
v.ExitWriteLock ();
}
示例14: 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();
}
}
示例15: 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);
}