本文整理汇总了C#中System.Threading.ReaderWriterLockSlim.EnterReadLock方法的典型用法代码示例。如果您正苦于以下问题:C# ReaderWriterLockSlim.EnterReadLock方法的具体用法?C# ReaderWriterLockSlim.EnterReadLock怎么用?C# ReaderWriterLockSlim.EnterReadLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.ReaderWriterLockSlim
的用法示例。
在下文中一共展示了ReaderWriterLockSlim.EnterReadLock方法的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: Dispose_WithReadLock
public void Dispose_WithReadLock ()
{
var rwl = new ReaderWriterLockSlim ();
rwl.EnterReadLock ();
try {
rwl.Dispose ();
Assert.Fail ("1");
} catch (SynchronizationLockException) {
}
}
示例3: ReadLock
public ReadLock(ReaderWriterLockSlim rwl, bool upgradeable = false)
{
this.rwl = rwl;
this.upgradeable = upgradeable;
if (upgradeable)
rwl.EnterUpgradeableReadLock();
else
rwl.EnterReadLock();
}
示例4: SlimReadLockHolder
public SlimReadLockHolder(ReaderWriterLockSlim locker, bool waitForLock)
{
this.locker = locker;
if(waitForLock)
{
locker.EnterReadLock();
lockAcquired = true;
return;
}
lockAcquired = locker.TryEnterReadLock(0);
}
示例5: 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();
}
示例6: 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();
}
}
示例7: 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];
}
示例8: OneManyLockIsFasterThanReaderWriterLockSlim
public void OneManyLockIsFasterThanReaderWriterLockSlim()
{
// OneManyLock.
var sw = Stopwatch.StartNew();
using (var oml = new OneManyLock())
{
oml.Enter(true);
M();
oml.Leave(true);
for (Int32 i = 0; i < c_iterations; i++)
{
oml.Enter(true);
M();
oml.Leave(true);
}
}
Int64 omlElapsedMilliseconds = sw.ElapsedMilliseconds;
// ReaderWriterLockSlim.
sw.Restart();
using (var rwls = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion))
{
rwls.EnterReadLock();
M();
rwls.ExitReadLock();
for (Int32 i = 0; i < c_iterations; i++)
{
rwls.EnterReadLock();
M();
rwls.ExitReadLock();
}
}
Int64 rwlsElapsedMilliseconds = sw.ElapsedMilliseconds;
Assert.True(omlElapsedMilliseconds < rwlsElapsedMilliseconds);
}
示例9: TestMethod2
public void TestMethod2()
{
ISomething something = new Something();
var tasks = new List<Task>();
var rwlock = new ReaderWriterLockSlim();
for (var i = 0; i < 100; i++)
{
// These threads should never see "nothing"....
tasks.Add(Task.Run(() =>
{
for (var x = 0; x < 1000000; x++)
{
rwlock.EnterReadLock();
try
{
Thread.Sleep(100);
Assert.That(something.IsNothing, Is.False);
}
finally
{
rwlock.ExitReadLock();
}
}
}));
}
// ... even though this one keeps setting it to nothing
tasks.Add(Task.Run(() =>
{
for (var x = 0; x < 1000000; x++)
{
rwlock.EnterWriteLock();
try
{
something = new Nothing();
Thread.Sleep(100);
something = new Something();
}
finally
{
rwlock.ExitWriteLock();
}
}
}));
Task.WhenAll(tasks).Wait();
}
示例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: NestableReadLock
/// <summary>
/// Initializes a new instance of the <see cref="ReadLock"/> class.
/// </summary>
/// <param name="readerWriterLockSlim">The reader writer lock slim.</param>
public NestableReadLock(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.IsReadLockHeld
|| readerWriterLockSlim.IsUpgradeableReadLockHeld
|| readerWriterLockSlim.IsWriteLockHeld)
{
lockAcquired = false;
}
else
{
readerWriterLockSlim.EnterReadLock();
lockAcquired = true;
}
}
示例12: 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();
}
}
示例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: EnterReadLock_MultiRead
public void EnterReadLock_MultiRead ()
{
var v = new ReaderWriterLockSlim ();
int local = 10;
var r = from i in Enumerable.Range (1, 30) select new Thread (() => {
// Just to cause some contention
Thread.Sleep (100);
v.EnterReadLock ();
Assert.AreEqual (10, local);
});
var threads = r.ToList ();
foreach (var t in threads) {
t.Start ();
}
foreach (var t in threads) {
// Console.WriteLine (t.ThreadState);
t.Join ();
}
}
示例15: TryEnterWriteLock_WhileReading
public void TryEnterWriteLock_WhileReading ()
{
var v = new ReaderWriterLockSlim ();
AutoResetEvent ev = new AutoResetEvent (false);
AutoResetEvent ev2 = new AutoResetEvent (false);
Thread t1 = new Thread (() => {
v.EnterReadLock ();
ev2.Set ();
ev.WaitOne ();
v.ExitReadLock ();
});
t1.Start ();
ev2.WaitOne ();
Assert.IsFalse (v.TryEnterWriteLock (100));
Assert.IsFalse (v.TryEnterReadLock (100));
ev.Set ();
Assert.IsTrue (v.TryEnterWriteLock (100));
}