Created
April 15, 2022 08:58
-
-
Save mirasrael/6cec659ab0d5e6b0d120fe6ebc684ecf to your computer and use it in GitHub Desktop.
Read Write Lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Sample | |
{ | |
ReadWriteLockSlim rwLock; | |
int counter; | |
public void IncrementTwice() | |
{ | |
rwLock.EnterWriteLock(); | |
try | |
{ | |
counter = counter + 1; | |
counter = counter + 1; | |
} | |
finally | |
{ | |
rwLock.ExitWriteLock(); | |
} | |
} | |
public int Get() | |
{ | |
rwLock.EnterReadLock(); | |
try | |
{ | |
return counter; | |
} | |
finally | |
{ | |
rwLock.ExitReadLock(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment