本文整理汇总了C#中System.Threading.LockRecursionException类的典型用法代码示例。如果您正苦于以下问题:C# LockRecursionException类的具体用法?C# LockRecursionException怎么用?C# LockRecursionException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LockRecursionException类属于System.Threading命名空间,在下文中一共展示了LockRecursionException类的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.
static ReaderWriterLockSlim rwls = new ReaderWriterLockSlim();
static void ThreadProc()
{
Console.WriteLine("Acquire the reader lock.");
rwls.EnterReadLock();
try
{
Console.WriteLine("\nAttempt to acquire the reader lock recursively:");
rwls.EnterReadLock();
}
catch (LockRecursionException lre)
{
Console.WriteLine("{0}: {1}",
lre.GetType().Name, lre.Message);
}
try
{
Console.WriteLine("\nAttempt to acquire the writer lock recursively:");
rwls.EnterWriteLock();
}
catch (LockRecursionException lre)
{
Console.WriteLine("{0}: {1}",
lre.GetType().Name, lre.Message);
}
}
static void Main()
{
Thread t = new Thread(ThreadProc);
t.Start();
t.Join();
}
}
输出:
Acquire the reader lock. Attempt to acquire the reader lock recursively: LockRecursionException: Recursive read lock acquisitions not allowed in this mode. Attempt to acquire the writer lock recursively: LockRecursionException: Write lock may not be acquired with read lock held. This pattern is prone to deadlocks. Consider using the upgrade lock.