本文整理汇总了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.
开发者ID:.NET开发者,项目名称:System.Threading,代码行数:89,代码来源:LockRecursionPolicy
注:本文中的System.Threading.LockRecursionPolicy枚举示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论