本文整理汇总了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();
}
});
}
示例2: SlimWriteLockHolder
public SlimWriteLockHolder(ReaderWriterLockSlim locker, bool waitForLock)
{
this.locker = locker;
if(waitForLock)
{
locker.EnterWriteLock();
lockAcquired = true;
return;
}
lockAcquired = locker.TryEnterWriteLock(0);
}
示例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();
}
示例4: AtomWrite
private static void AtomWrite(ReaderWriterLockSlim readerWriterLockSlim, Action action)
{
readerWriterLockSlim.EnterWriteLock();
try
{
action();
}
finally
{
readerWriterLockSlim.ExitWriteLock();
}
}
示例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();
}
}
示例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];
}
示例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++;
}
});
});
}
示例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;
}
}
示例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();
});
}
示例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;
}
}
示例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();
}
}
示例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();
}
示例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());
}
}
示例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) {
}
}
示例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");
}
}