本文整理汇总了C#中System.Threading.ReaderWriterLock.AcquireReaderLock方法的典型用法代码示例。如果您正苦于以下问题:C# ReaderWriterLock.AcquireReaderLock方法的具体用法?C# ReaderWriterLock.AcquireReaderLock怎么用?C# ReaderWriterLock.AcquireReaderLock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.ReaderWriterLock
的用法示例。
在下文中一共展示了ReaderWriterLock.AcquireReaderLock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: LockOnReaderWriterLock
private static void LockOnReaderWriterLock()
{
Console.WriteLine("About to lock on the ReaderWriterLock. Debug after seeing \"Signaled to acquire the reader lock.\"");
ReaderWriterLock rwLock = new ReaderWriterLock();
ManualResetEvent rEvent = new ManualResetEvent(false);
ManualResetEvent pEvent = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem((object state) =>
{
rwLock.AcquireWriterLock(-1);
Console.WriteLine("Writer lock acquired!");
rEvent.Set();
pEvent.WaitOne();
});
rEvent.WaitOne();
Console.WriteLine("Signaled to acquire the reader lock.");
rwLock.AcquireReaderLock(-1);
Console.WriteLine("Reader lock acquired");
pEvent.Set();
Console.WriteLine("About to end the program. Press any key to exit.");
Console.ReadKey();
}
示例2: ReaderLock
public ReaderLock(ReaderWriterLock rwLock)
{
if (null == rwLock) throw new Exception("Don't pass a null ReaderWriterLock object!");
_lock = rwLock;
_lock.AcquireReaderLock(Timeout.Infinite);
}
示例3: ExecuteJob
/// <summary>
/// Executes a job on demand, rather than waiting for its regularly scheduled time.
/// </summary>
/// <param name="job">The job to be executed.</param>
public static void ExecuteJob(JobBase job)
{
ReaderWriterLock rwLock = new ReaderWriterLock();
try
{
rwLock.AcquireReaderLock(Timeout.Infinite);
if (job.Executing == false)
{
LockCookie lockCookie = rwLock.UpgradeToWriterLock(Timeout.Infinite);
try
{
if (job.Executing == false)
{
job.Executing = true;
QueueJob(job);
}
}
finally
{
rwLock.DowngradeFromWriterLock(ref lockCookie);
}
}
}
finally
{
rwLock.ReleaseReaderLock();
}
}
示例4: TestIsReaderLockHeld
public void TestIsReaderLockHeld ()
{
rwlock = new ReaderWriterLock ();
Assert.IsTrue (!rwlock.IsReaderLockHeld, "#1");
rwlock.AcquireReaderLock (500);
Assert.IsTrue (rwlock.IsReaderLockHeld, "#1");
RunThread (new ThreadStart (IsReaderLockHeld_2));
rwlock.ReleaseReaderLock ();
}
示例5: TestIsReaderLockHeld
public void TestIsReaderLockHeld ()
{
rwlock = new ReaderWriterLock ();
Assert ("a1", !rwlock.IsReaderLockHeld);
rwlock.AcquireReaderLock (500);
Assert ("a2", rwlock.IsReaderLockHeld);
RunThread (new ThreadStart (IsReaderLockHeld_2));
rwlock.ReleaseReaderLock ();
}
示例6: LockingObject
public LockingObject(ReaderWriterLock _lock, AccessMode _lockMode)
{
readerWriterLock = _lock;
accessMode = _lockMode;
if (accessMode == AccessMode.Read)
{
readerWriterLock.AcquireReaderLock(-1);
}
else
{
readerWriterLock.AcquireWriterLock(-1);
}
}
示例7: ReadLock
private ReadLock( ReaderWriterLock syncLock, TimeSpan timeout )
{
if( syncLock == null )
throw new ArgumentNullException( "ReaderWriterLock syncLock" );
_rwLock = syncLock;
try
{
syncLock.AcquireReaderLock( timeout );
}
catch( ApplicationException )
{
_timedOut = true;
}
}
示例8: ReaderAutoLocker
/// <summary>
/// Obtain read access for the given object.
/// Throws ApplicationException if access has not been acquired within 30 secs.
/// </summary>
/// <param name="locker">Locker to acquire rights.</param>
public ReaderAutoLocker(ReaderWriterLock locker)
{
this.wasFreed = false;
this.locker = locker;
this.wasHeld = true;
if (this.locker.IsReaderLockHeld || this.locker.IsWriterLockHeld)
{
this.wasHeld = false;
return ;
}
locker.AcquireReaderLock(30000);
if (! locker.IsReaderLockHeld)
throw new ApplicationException("Can't acquire reader rights. CPU is overloaded or deadlock.");
}
示例9: IICLockRegion
public IICLockRegion(ReaderWriterLock innerLock, IICLockMode mode, int millisecondTimeout)
{
_innerLock = innerLock;
_lockMode = mode;
_msTimeout = millisecondTimeout;
switch (mode) {
case IICLockMode.ReaderLock:
_innerLock.AcquireReaderLock(millisecondTimeout);
break;
case IICLockMode.WriterLock:
_innerLock.AcquireWriterLock(millisecondTimeout);
break;
default:
throw new NotSupportedException("Unexcepted LockMode: " + mode);
}
}
示例10: ReaderWriter_When2CycleDetected_DoesNotThrows
public void ReaderWriter_When2CycleDetected_DoesNotThrows()
{
ReaderWriterLock rwl = new ReaderWriterLock();
Barrier barrier = new Barrier(2);
Action t1 = () =>
{
rwl.AcquireReaderLock(Timeout.Infinite);
barrier.SignalAndWait();
rwl.UpgradeToWriterLock(Timeout.Infinite);
rwl.ReleaseWriterLock();
};
Action t2 = () =>
{
rwl.AcquireReaderLock(Timeout.Infinite);
barrier.SignalAndWait();
Thread.Sleep(500);
rwl.ReleaseReaderLock();
};
TestHelpers.InvokeSimultaneouslyAndWait(t1, t2);
}
示例11: TestAcquireLocks
public void TestAcquireLocks ()
{
rwlock = new ReaderWriterLock ();
rwlock.AcquireReaderLock (500);
rwlock.AcquireReaderLock (500);
rwlock.ReleaseReaderLock ();
Assert.IsTrue (rwlock.IsReaderLockHeld, "#1");
RunThread (new ThreadStart (AcquireLock_readerWorks));
Assert.IsTrue (rwlock.IsReaderLockHeld);
RunThread (new ThreadStart (AcquireLock_writerFails));
rwlock.ReleaseReaderLock ();
Assert.IsTrue (!rwlock.IsReaderLockHeld);
RunThread (new ThreadStart (AcquireLock_writerWorks));
rwlock.AcquireWriterLock (200);
RunThread (new ThreadStart (AcquireLock_writerFails));
RunThread (new ThreadStart (AcquireLock_readerFails));
rwlock.ReleaseWriterLock ();
}
示例12: TestBug_55909
public void TestBug_55909 ()
{
rwlock = new ReaderWriterLock ();
ThreadRunner tr = StartThread (new ThreadStart(Bug_55909_Thread2));
Thread.Sleep (200);
rwlock.AcquireReaderLock (Timeout.Infinite);
try {
LockCookie lc = rwlock.UpgradeToWriterLock (Timeout.Infinite);
Thread.Sleep (500);
}
finally { rwlock.ReleaseReaderLock(); }
tr.Join ();
}
示例13: DowngradeTest
public void DowngradeTest ()
{
LockCookie lc1, lc2, lc3, lc4;
rwlock = new ReaderWriterLock ();
rwlock.AcquireReaderLock (Timeout.Infinite);
lc1 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
rwlock.AcquireReaderLock (Timeout.Infinite);
lc2 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
rwlock.AcquireReaderLock (Timeout.Infinite);
lc3 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
rwlock.AcquireReaderLock (Timeout.Infinite);
lc4 = rwlock.UpgradeToWriterLock (Timeout.Infinite);
rwlock.DowngradeFromWriterLock (ref lc2);
Assert.IsFalse (rwlock.IsReaderLockHeld, "A1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "A2");
rwlock.ReleaseReaderLock ();
Assert.IsFalse (rwlock.IsReaderLockHeld, "B1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "B2");
rwlock.DowngradeFromWriterLock (ref lc4);
Assert.IsFalse (rwlock.IsReaderLockHeld, "C1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "C2");
rwlock.ReleaseReaderLock ();
Assert.IsFalse (rwlock.IsReaderLockHeld, "D1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "D2");
rwlock.DowngradeFromWriterLock (ref lc3);
Assert.IsFalse (rwlock.IsReaderLockHeld, "E1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "E2");
rwlock.ReleaseReaderLock ();
Assert.IsFalse (rwlock.IsReaderLockHeld, "F1");
Assert.IsTrue (rwlock.IsWriterLockHeld, "F2");
rwlock.DowngradeFromWriterLock (ref lc1);
Assert.IsTrue (rwlock.IsReaderLockHeld, "G1");
Assert.IsFalse (rwlock.IsWriterLockHeld, "G2");
rwlock.ReleaseReaderLock ();
Assert.IsFalse (rwlock.IsReaderLockHeld, "H1");
Assert.IsFalse (rwlock.IsWriterLockHeld, "H2");
}
示例14: TestReaderInsideWriter
public void TestReaderInsideWriter ()
{
// Reader acquires and releases work like the writer equivalent
rwlock = new ReaderWriterLock ();
rwlock.AcquireWriterLock (-1);
rwlock.AcquireReaderLock (-1);
Assert ("u1", !rwlock.IsReaderLockHeld);
Assert ("u2", rwlock.IsWriterLockHeld);
rwlock.AcquireReaderLock (-1);
Assert ("u3", !rwlock.IsReaderLockHeld);
Assert ("u4", rwlock.IsWriterLockHeld);
rwlock.ReleaseWriterLock ();
Assert ("u5", !rwlock.IsReaderLockHeld);
Assert ("u6", rwlock.IsWriterLockHeld);
rwlock.ReleaseReaderLock ();
Assert ("u7", !rwlock.IsReaderLockHeld);
Assert ("u8", rwlock.IsWriterLockHeld);
rwlock.ReleaseReaderLock ();
Assert ("u9", !rwlock.IsReaderLockHeld);
Assert ("u10", !rwlock.IsWriterLockHeld);
}
示例15: ReadLocker
public ReadLocker(ref ReaderWriterLock rwl)
{
mRwl = rwl;
mRwl.AcquireReaderLock(Constants.kLockTimoutMs);
}