本文整理匯總了C#中System.Threading.ReaderWriterLock.ReleaseWriterLock方法的典型用法代碼示例。如果您正苦於以下問題:C# ReaderWriterLock.ReleaseWriterLock方法的具體用法?C# ReaderWriterLock.ReleaseWriterLock怎麽用?C# ReaderWriterLock.ReleaseWriterLock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Threading.ReaderWriterLock
的用法示例。
在下文中一共展示了ReaderWriterLock.ReleaseWriterLock方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: getOneCell
public static string getOneCell()
{
string encodedOrder=null;
readSem.WaitOne();// wait until buffer cell is not empty then only read
ReaderWriterLock rw=new ReaderWriterLock();
rw.AcquireWriterLock(Timeout.Infinite);
try{
//reading from buffer --- ciruclar queue
if (front_read == bufferSize - 1)
front_read = 0;
else
{
front_read = front_read + 1;
}
encodedOrder = buffer[front_read];
buffer[front_read] = string.Empty;
String[] arr=encodedOrder.Split('#');
Console.WriteLine(" chicken farm got an order from {0} of {1} chicken",arr[0],arr[2]);
}
catch (Exception e){ Console.WriteLine(""); }
finally
{
rw.ReleaseWriterLock();
writeSem.Release();
}
return encodedOrder;
}
示例2: TestIsWriterLockHeld
public void TestIsWriterLockHeld ()
{
rwlock = new ReaderWriterLock ();
Assert.IsTrue (!rwlock.IsWriterLockHeld, "#1");
rwlock.AcquireWriterLock (500);
Assert.IsTrue (rwlock.IsWriterLockHeld, "#2");
RunThread (new ThreadStart (IsWriterLockHeld_2));
rwlock.ReleaseWriterLock ();
}
示例3: 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();
});
}
示例4: PurgeCache
protected override void PurgeCache(object sender, System.Timers.ElapsedEventArgs e)
{
ReaderWriterLock readWriteLock = new ReaderWriterLock();
//Öncelik olarak ortanın altında seçiyoruz. Çünkü öncelik ekleme, güncelleme gibi işlerde.
//Varsın bi kaç saniye geç temizlensin cache
Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
if (!Monitor.TryEnter(isPurging))
return;
try
{
readWriteLock.AcquireWriterLock(500);
try
{
List<object> expiredItems = new List<object>();
foreach (CacheKey timedKey in timedStorage.Keys)
{
if (timedKey.ExpirationDate < e.SignalTime)
{
// Temizlenmesi için birnevi nesneler işaretlenir.
expiredItems.Add(timedKey.Key);
}
else
break;
}
foreach (object key in expiredItems)
{
CacheKey timedKey = timedStorageIndex[key];
timedStorageIndex.Remove(timedKey.Key);
timedStorage.Remove(timedKey);
}
}
catch (ApplicationException ae)
{
System.Console.WriteLine("Şu an lock lı.");
}
finally
{
readWriteLock.ReleaseWriterLock();
}
}
finally { Monitor.Exit(isPurging); }
}
示例5: setOneCell
public static void setOneCell(string encodedObject)
{
writeSem.WaitOne(); //wait until buffer has atleast one space in it
ReaderWriterLock rw = new ReaderWriterLock();
rw.AcquireWriterLock(Timeout.Infinite);
try{ // writing into buffer --- circular queue
if(rear_write==bufferSize-1)
rear_write=0;
else
rear_write=rear_write+1;
buffer[rear_write]=encodedObject;
}
finally
{
rw.ReleaseWriterLock();
readSem.Release();
}
}
示例6: 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 ();
}
示例7: TestReaderInsideWriter
public void TestReaderInsideWriter ()
{
// Reader acquires and releases work like the writer equivalent
rwlock = new ReaderWriterLock ();
rwlock.AcquireWriterLock (-1);
rwlock.AcquireReaderLock (-1);
Assert.IsTrue (!rwlock.IsReaderLockHeld);
Assert.IsTrue (rwlock.IsWriterLockHeld);
rwlock.AcquireReaderLock (-1);
Assert.IsTrue (!rwlock.IsReaderLockHeld);
Assert.IsTrue (rwlock.IsWriterLockHeld);
rwlock.ReleaseWriterLock ();
Assert.IsTrue (!rwlock.IsReaderLockHeld);
Assert.IsTrue (rwlock.IsWriterLockHeld);
rwlock.ReleaseReaderLock ();
Assert.IsTrue (!rwlock.IsReaderLockHeld);
Assert.IsTrue (rwlock.IsWriterLockHeld);
rwlock.ReleaseReaderLock ();
Assert.IsTrue (!rwlock.IsReaderLockHeld);
Assert.IsTrue (!rwlock.IsWriterLockHeld);
}
示例8: TestReleaseRestoreWriterLock
public void TestReleaseRestoreWriterLock ()
{
rwlock = new ReaderWriterLock ();
rwlock.AcquireWriterLock (500);
rwlock.AcquireWriterLock (500);
Assert.IsTrue (rwlock.IsWriterLockHeld);
LockCookie co = rwlock.ReleaseLock ();
RunThread (new ThreadStart (AcquireLock_readerWorks));
rwlock.RestoreLock (ref co);
RunThread (new ThreadStart (AcquireLock_readerFails));
rwlock.ReleaseWriterLock ();
Assert.IsTrue (rwlock.IsWriterLockHeld);
rwlock.ReleaseWriterLock ();
Assert.IsTrue (!rwlock.IsWriterLockHeld);
}
示例9: AcquireWriterLock
public static IDisposable AcquireWriterLock(ReaderWriterLock rwl)
{
rwl.AcquireWriterLock(Timeout.Infinite);
return new Disposer(() => rwl.ReleaseWriterLock());
}
示例10: ReaderWriter_WhenUpgradedWriteLockReleased_EdgesDeleteFromGraph
public void ReaderWriter_WhenUpgradedWriteLockReleased_EdgesDeleteFromGraph()
{
ReaderWriterLock rwl = new ReaderWriterLock();
Barrier barrier = new Barrier(3);
Barrier barrier2 = new Barrier(3);
Task t1 = new Task(() =>
{
rwl.AcquireReaderLock(Timeout.Infinite);
rwl.UpgradeToWriterLock(Timeout.Infinite);
rwl.ReleaseWriterLock();
barrier.SignalAndWait();
barrier2.SignalAndWait();
lock (rwl)
{
Thread.Sleep(100);
}
});
Task t2 = new Task(() =>
{
barrier.SignalAndWait();
lock (rwl)
{
barrier2.SignalAndWait();
rwl.AcquireWriterLock(Timeout.Infinite);
}
});
t1.Start();
t2.Start();
barrier.SignalAndWait();
rwl.AcquireReaderLock(Timeout.Infinite);
barrier2.SignalAndWait();
Thread.Sleep(500);
rwl.ReleaseReaderLock();
Task.WaitAll(new[] { t1, t2 });
}
示例11: 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);
}
示例12: TestIsWriterLockHeld
public void TestIsWriterLockHeld ()
{
rwlock = new ReaderWriterLock ();
Assert ("a1", !rwlock.IsWriterLockHeld);
rwlock.AcquireWriterLock (500);
Assert ("a2", rwlock.IsWriterLockHeld);
RunThread (new ThreadStart (IsWriterLockHeld_2));
rwlock.ReleaseWriterLock ();
}
示例13: 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);
}
示例14: WithWriteLockTimeoutWithExceptionTest
public void WithWriteLockTimeoutWithExceptionTest()
{
var l = new ReaderWriterLock();
var e = new AutoResetEvent(false);
var lockGetted = false;
var t = new Thread(() =>
{
var b = false;
try
{
l.WithWriteLock(() =>
{
lockGetted = true;
Assert.IsTrue(l.IsWriterLockHeld);
throw new InvalidOperationException();
}, TimeSpan.FromSeconds(5));
}
catch (InvalidOperationException)
{
b = true;
}
Assert.IsTrue(b);
e.Set();
});
l.AcquireWriterLock(100);
t.Start();
Thread.Sleep(1000);
Assert.IsFalse(lockGetted);
l.ReleaseWriterLock();
e.WaitOne();
Assert.IsTrue(lockGetted);
Assert.IsFalse(l.IsWriterLockHeld);
}
示例15: WithWriteLockTimeoutSuccessTest
public void WithWriteLockTimeoutSuccessTest()
{
var l = new ReaderWriterLock();
var e = new AutoResetEvent(false);
var lockGetted = false;
var t = new Thread(() =>
{
l.WithWriteLock(() =>
{
lockGetted = true;
Assert.IsTrue(l.IsWriterLockHeld);
}, TimeSpan.FromSeconds(5));
e.Set();
});
l.AcquireWriterLock(100);
t.Start();
Thread.Sleep(1000);
Assert.IsFalse(lockGetted);
l.ReleaseWriterLock();
e.WaitOne();
Assert.IsTrue(lockGetted);
Assert.IsFalse(l.IsWriterLockHeld);
}