C# System.Threading.ReaderWriterLockSlim
1 using System; 2 using System.Threading; 3 using System.Threading.Tasks; 4 using System.Collections.Generic; 5 6 public class SynchronizedCache 7 { 8 private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim(); 9 private Dictionary<int, string> innerCache = new Dictionary<int, string>(); 10 11 public int Count 12 { get { return innerCache.Count; } } 13 14 public string Read(int key) 15 { 16 cacheLock.EnterReadLock(); 17 try 18 { 19 return innerCache[key]; 20 } 21 finally 22 { 23 cacheLock.ExitReadLock(); 24 } 25 } 26 27 public void Add(int key, string value) 28 { 29 cacheLock.EnterWriteLock(); 30 try 31 { 32 innerCache.Add(key, value); 33 } 34 finally 35 { 36 cacheLock.ExitWriteLock(); 37 } 38 } 39 40 public bool AddWithTimeout(int key, string value, int timeout) 41 { 42 if (cacheLock.TryEnterWriteLock(timeout)) 43 { 44 try 45 { 46 innerCache.Add(key, value); 47 } 48 finally 49 { 50 cacheLock.ExitWriteLock(); 51 } 52 return true; 53 } 54 else 55 { 56 return false; 57 } 58 } 59 60 public AddOrUpdateStatus AddOrUpdate(int key, string value) 61 { 62 cacheLock.EnterUpgradeableReadLock(); 63 try 64 { 65 string result = null; 66 if (innerCache.TryGetValue(key, out result)) 67 { 68 if (result == value) 69 { 70 return AddOrUpdateStatus.Unchanged; 71 } 72 else 73 { 74 cacheLock.EnterWriteLock(); 75 try 76 { 77 innerCache[key] = value; 78 } 79 finally 80 { 81 cacheLock.ExitWriteLock(); 82 } 83 return AddOrUpdateStatus.Updated; 84 } 85 } 86 else 87 { 88 cacheLock.EnterWriteLock(); 89 try 90 { 91 innerCache.Add(key, value); 92 } 93 finally 94 { 95 cacheLock.ExitWriteLock(); 96 } 97 return AddOrUpdateStatus.Added; 98 } 99 } 100 finally 101 { 102 cacheLock.ExitUpgradeableReadLock(); 103 } 104 } 105 106 public void Delete(int key) 107 { 108 cacheLock.EnterWriteLock(); 109 try 110 { 111 innerCache.Remove(key); 112 } 113 finally 114 { 115 cacheLock.ExitWriteLock(); 116 } 117 } 118 119 public enum AddOrUpdateStatus 120 { 121 Added, 122 Updated, 123 Unchanged 124 }; 125 126 ~SynchronizedCache() 127 { 128 if (cacheLock != null) cacheLock.Dispose(); 129 } 130 }
1 using System; 2 using System.Threading; 3 using System.Threading.Tasks; 4 using System.Collections.Generic; 5 6 public class Example 7 { 8 public static void Main() 9 { 10 var sc = new SynchronizedCache(); 11 var tasks = new List<Task>(); 12 int itemsWritten = 0; 13 14 // Execute a writer. 15 tasks.Add(Task.Run( () => { String[] vegetables = { "broccoli", "cauliflower", 16 "carrot", "sorrel", "baby turnip", 17 "beet", "brussel sprout", 18 "cabbage", "plantain", 19 "spinach", "grape leaves", 20 "lime leaves", "corn", 21 "radish", "cucumber", 22 "raddichio", "lima beans" }; 23 for (int ctr = 1; ctr <= vegetables.Length; ctr++) 24 sc.Add(ctr, vegetables[ctr - 1]); 25 26 itemsWritten = vegetables.Length; 27 Console.WriteLine("Task {0} wrote {1} items\n", 28 Task.CurrentId, itemsWritten); 29 } )); 30 // Execute two readers, one to read from first to last and the second from last to first. 31 for (int ctr = 0; ctr <= 1; ctr++) { 32 bool desc = Convert.ToBoolean(ctr); 33 tasks.Add(Task.Run( () => { int start, last, step; 34 int items; 35 do { 36 String output = String.Empty; 37 items = sc.Count; 38 if (! desc) { 39 start = 1; 40 step = 1; 41 last = items; 42 } 43 else { 44 start = items; 45 step = -1; 46 last = 1; 47 } 48 49 for (int index = start; desc ? index >= last : index <= last; index += step) 50 output += String.Format("[{0}] ", sc.Read(index)); 51 52 Console.WriteLine("Task {0} read {1} items: {2}\n", 53 Task.CurrentId, items, output); 54 } while (items < itemsWritten | itemsWritten == 0); 55 } )); 56 } 57 // Execute a red/update task. 58 tasks.Add(Task.Run( () => { Thread.Sleep(100); 59 for (int ctr = 1; ctr <= sc.Count; ctr++) { 60 String value = sc.Read(ctr); 61 if (value == "cucumber") 62 if (sc.AddOrUpdate(ctr, "green bean") != SynchronizedCache.AddOrUpdateStatus.Unchanged) 63 Console.WriteLine("Changed 'cucumber' to 'green bean'"); 64 } 65 } )); 66 67 // Wait for all three tasks to complete. 68 Task.WaitAll(tasks.ToArray()); 69 70 // Display the final contents of the cache. 71 Console.WriteLine(); 72 Console.WriteLine("Values in synchronized cache: "); 73 for (int ctr = 1; ctr <= sc.Count; ctr++) 74 Console.WriteLine(" {0}: {1}", ctr, sc.Read(ctr)); 75 76 } 77 } 78 // The example displays the following output: 79 // Task 1 read 0 items: 80 // 81 // Task 3 wrote 17 items 82 // 83 // 84 // Task 1 read 17 items: [broccoli] [cauliflower] [carrot] [sorrel] [baby turnip] [ 85 // beet] [brussel sprout] [cabbage] [plantain] [spinach] [grape leaves] [lime leave 86 // s] [corn] [radish] [cucumber] [raddichio] [lima beans] 87 // 88 // Task 2 read 0 items: 89 // 90 // Task 2 read 17 items: [lima beans] [raddichio] [cucumber] [radish] [corn] [lime 91 // leaves] [grape leaves] [spinach] [plantain] [cabbage] [brussel sprout] [beet] [b 92 // aby turnip] [sorrel] [carrot] [cauliflower] [broccoli] 93 // 94 // Changed 'cucumber' to 'green bean' 95 // 96 // Values in synchronized cache: 97 // 1: broccoli 98 // 2: cauliflower 99 // 3: carrot 100 // 4: sorrel 101 // 5: baby turnip 102 // 6: beet 103 // 7: brussel sprout 104 // 8: cabbage 105 // 9: plantain 106 // 10: spinach 107 // 11: grape leaves 108 // 12: lime leaves 109 // 13: corn 110 // 14: radish 111 // 15: green bean 112 // 16: raddichio 113 // 17: lima beans
构造函数
ReaderWriterLockSlim() |
使用默认属性值初始化 ReaderWriterLockSlim 类的新实例。 |
ReaderWriterLockSlim(LockRecursionPolicy) |
在指定锁定递归策略的情况下初始化 ReaderWriterLockSlim 类的新实例。 |
属性
CurrentReadCount |
获取已进入读取模式锁定状态的独有线程的总数。 |
IsReadLockHeld |
获取一个值,该值指示当前线程是否已进入读取模式的锁定状态。 |
IsUpgradeableReadLockHeld |
获取一个值,该值指示当前线程是否已进入可升级模式的锁定状态。 |
IsWriteLockHeld |
获取一个值,该值指示当前线程是否已进入写入模式的锁定状态。 |
RecursionPolicy |
获取一个值,该值指示当前 ReaderWriterLockSlim 对象的递归策略。 |
RecursiveReadCount |
获取当前线程进入读取模式锁定状态的次数,用于指示递归。 |
RecursiveUpgradeCount |
获取当前线程进入可升级模式锁定状态的次数,用于指示递归。 |
RecursiveWriteCount |
获取当前线程进入写入模式锁定状态的次数,用于指示递归。 |
WaitingReadCount |
获取等待进入读取模式锁定状态的线程总数。 |
WaitingUpgradeCount |
获取等待进入可升级模式锁定状态的线程总数。 |
WaitingWriteCount |
获取等待进入写入模式锁定状态的线程总数。 |
方法
Dispose() |
释放 ReaderWriterLockSlim 类的当前实例所使用的所有资源。 |
EnterReadLock() |
尝试进入读取模式锁定状态。 |
EnterUpgradeableReadLock() |
尝试进入可升级模式锁定状态。 |
EnterWriteLock() |
尝试进入写入模式锁定状态。 |
Equals(Object) |
确定指定的对象是否等于当前对象。 (Inherited from Object) |
ExitReadLock() |
减少读取模式的递归计数,并在生成的计数为 0(零)时退出读取模式。 |
ExitUpgradeableReadLock() |
减少可升级模式的递归计数,并在生成的计数为 0(零)时退出可升级模式。 |
ExitWriteLock() |
减少写入模式的递归计数,并在生成的计数为 0(零)时退出写入模式。 |
GetHashCode() |
作为默认哈希函数。 (Inherited from Object) |
GetType() |
获取当前实例的 Type。 (Inherited from Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (Inherited from Object) |
ToString() |
返回表示当前对象的字符串。 (Inherited from Object) |
TryEnterReadLock(Int32) |
尝试进入读取模式锁定状态,可以选择整数超时时间。 |
TryEnterReadLock(TimeSpan) |
尝试进入读取模式锁定状态,可以选择超时时间。 |
TryEnterUpgradeableReadLock(Int32) |
尝试进入可升级模式锁定状态,可以选择超时时间。 |
TryEnterUpgradeableReadLock(TimeSpan) |
尝试进入可升级模式锁定状态,可以选择超时时间。 |
TryEnterWriteLock(Int32) |
尝试进入写入模式锁定状态,可以选择超时时间。 |
TryEnterWriteLock(TimeSpan) |
尝试进入写入模式锁定状态,可以选择超时时间。 |