|
1 | 1 | # Open.Threading.ReadWrite |
2 | 2 |
|
3 | | -Useful set of extensions and classes for simplifying and optimizing read-write synchronization. |
| 3 | +Useful set of extensions and classes for simplifying and optimizing read-write synchronization with `ReaderWriterLockSlim`. |
4 | 4 |
|
| 5 | +[](https://www.nuget.org/packages/Open.Threading.ReadWrite/blob/master/LICENSE) |
| 6 | + |
5 | 7 | [](https://www.nuget.org/packages/Open.Threading.ReadWrite/) |
| 8 | + |
| 9 | +## Purpose |
| 10 | + |
| 11 | +There are very common but tedious patterns to follow when utilizing a `ReaderWriterLockSlim`. Some of the more complex but useful patterns involve reading (with or without a read lock) and then if a condition is met, acquiring a write lock before proceeding. |
| 12 | + |
| 13 | +This extension library removes the tediousness of properly acquiring and releasing various locks and provides easy access to timeout values if needed. |
| 14 | + |
| 15 | +## Basics |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +### Read |
| 20 | + |
| 21 | +```cs |
| 22 | +rwLockSlim.Read(() => |
| 23 | +{ |
| 24 | + /* do work inside a read lock */ |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +```cs |
| 29 | +using(rwLockSlim.ReadLock()) |
| 30 | +{ |
| 31 | + /* do work inside a read lock */ |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +```cs |
| 36 | +int result = rwLockSlim.Read(() => |
| 37 | +{ |
| 38 | + int i; |
| 39 | + /* produce a result inside read lock */ |
| 40 | + return i; |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +### Write |
| 47 | + |
| 48 | +```cs |
| 49 | +rwLockSlim.Write(() => |
| 50 | +{ |
| 51 | + /* do work inside a read lock */ |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +```cs |
| 56 | +using(rwLockSlim.WriteLock()) |
| 57 | +{ |
| 58 | + /* do work inside a write lock */ |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +```cs |
| 63 | +int result = rwLockSlim.Write(() => |
| 64 | +{ |
| 65 | + int i; |
| 66 | + /* produce a result inside write lock */ |
| 67 | + return i; |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +### Upgradable Read |
| 74 | + |
| 75 | +```cs |
| 76 | +using(rwLockSlim.UpgradableReadLock()) |
| 77 | +{ |
| 78 | + /* do work inside an upgradable read lock */ |
| 79 | + if(condition) |
| 80 | + { |
| 81 | + using(rwLockSlim.WriteLock()) |
| 82 | + { |
| 83 | + /* upgraded to a write lock */ |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +## With Timeouts |
| 91 | + |
| 92 | +These throw a `TimeoutException` if a lock cannot be acquired within the time specified. |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +### Read |
| 97 | + |
| 98 | +```cs |
| 99 | +rwLockSlim.Read(1000 /* ms */, () => |
| 100 | +{ |
| 101 | + /* do work inside a read lock */ |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +or |
| 106 | + |
| 107 | +```cs |
| 108 | +using(rwLockSlim.ReadLock(1000)) // ms |
| 109 | +{ |
| 110 | + /* do work inside a read lock */ |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +### Write |
| 117 | + |
| 118 | +```cs |
| 119 | +rwLockSlim.Write(1000 /* ms */, () => |
| 120 | +{ |
| 121 | + /* do work inside a write lock */ |
| 122 | +}); |
| 123 | +``` |
| 124 | + |
| 125 | +or |
| 126 | + |
| 127 | +```cs |
| 128 | +using(rwLockSlim.WriteLock(1000)) // ms |
| 129 | +{ |
| 130 | + /* do work inside a write lock */ |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Advanced Examples |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +### WriteConditional |
| 139 | + |
| 140 | +This example demonstrates how to properly query a value before writing with a 1 second timeout. |
| 141 | + |
| 142 | +```cs |
| 143 | +var actionWasInvoked = rwLockSlim.WriteConditional(1000 /* ms */, |
| 144 | +() => /* condition that is queried inside an upgradable read lock */, |
| 145 | +() => /* do work inside a write lock */); |
| 146 | +``` |
| 147 | + |
| 148 | +### ReadWriteConditional |
| 149 | + |
| 150 | +This more advanced example optimizes the process of reading and then writing by first testing the condition within a read lock before attempting with an upgradable read lock. |
| 151 | + |
| 152 | +```cs |
| 153 | +int result = 0; |
| 154 | +bool actionWasInvoked = rwLockSlim.ReadWriteConditional(ref result, |
| 155 | +isUpgraded => /* condition that is first queried inside a read lock */, |
| 156 | +() => |
| 157 | +{ |
| 158 | + int i; |
| 159 | + /* do work inside a write lock */ |
| 160 | + return i; |
| 161 | +}); |
| 162 | +``` |
0 commit comments