本文整理匯總了C#中System.Threading.LockRecursionPolicy枚舉的典型用法代碼示例。如果您正苦於以下問題:C# LockRecursionPolicy枚舉的具體用法?C# LockRecursionPolicy怎麽用?C# LockRecursionPolicy使用的例子?那麽, 這裏精選的枚舉代碼示例或許可以為您提供幫助。
在下文中一共展示了LockRecursionPolicy枚舉的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ReaderWriterLockSlim
//引入命名空間
using System;
using System.Threading;
class Example
{
// By default, the lock recursion policy for a new
// ReaderWriterLockSlim does not allow recursion.
private static ReaderWriterLockSlim rwls = new ReaderWriterLockSlim();
private static ReaderWriterLockSlim rwlsWithRecursion =
new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
static void ThreadProc()
{
Console.WriteLine("1. Enter the read lock recursively.");
ReadRecursive(rwls);
ReadRecursive(rwlsWithRecursion);
Console.WriteLine("\n2. Enter the write lock recursively from the read lock.");
ReadWriteRecursive(rwls);
ReadWriteRecursive(rwlsWithRecursion);
}
static void ReadRecursive(ReaderWriterLockSlim rwls)
{
Console.WriteLine("LockRecursionPolicy.{0}:", rwls.RecursionPolicy);
rwls.EnterReadLock();
try {
rwls.EnterReadLock();
Console.WriteLine("\nThe read lock was entered recursively.");
rwls.ExitReadLock();
}
catch (LockRecursionException lre) {
Console.WriteLine("\n{0}: {1}",
lre.GetType().Name, lre.Message);
}
rwls.ExitReadLock();
}
static void ReadWriteRecursive(ReaderWriterLockSlim rwls)
{
Console.WriteLine("LockRecursionPolicy.{0}:", rwls.RecursionPolicy);
rwls.EnterReadLock();
try {
rwls.EnterWriteLock();
Console.WriteLine("\nThe write lock was entered recursively.");
}
catch (LockRecursionException lre) {
Console.WriteLine("\n{0}: {1}",
lre.GetType().Name, lre.Message);
}
rwls.ExitReadLock();
}
static void Main()
{
Thread t = new Thread(ThreadProc);
t.Start();
t.Join();
// Dispose of ReaderWriterLockSlim objects' unmanaged resources.
if (rwls != null) rwls.Dispose();
if (rwlsWithRecursion != null) rwlsWithRecursion.Dispose();
}
}
// This example displays output similar to the following:
// 1. Enter the read lock recursively.
// LockRecursionPolicy.NoRecursion:
//
// LockRecursionException: Recursive read lock acquisitions not allowed in this mode.
// LockRecursionPolicy.SupportsRecursion:
//
// The read lock was entered recursively.
//
// 2. Enter the write lock recursively from the read lock.
// LockRecursionPolicy.NoRecursion:
//
// LockRecursionException: Write lock may not be acquired with read lock held. This pattern i
// s prone to deadlocks. Please ensure that read locks are released before taking a write loc
// k. If an upgrade is necessary, use an upgrade lock in place of the read lock.
// LockRecursionPolicy.SupportsRecursion:
//
// LockRecursionException: Write lock may not be acquired with read lock held. This pattern i
// s prone to deadlocks. Please ensure that read locks are released before taking a write loc
// k. If an upgrade is necessary, use an upgrade lock in place of the read lock.