Last active
April 15, 2022 09:09
-
-
Save mirasrael/260d851f32d14e5a0d07800f338fca37 to your computer and use it in GitHub Desktop.
Mutex
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 | |
{ | |
object mutex = new object(); | |
int counter; | |
public void IncrementTwice() | |
{ | |
lock (mutex) | |
{ | |
counter = counter + 1; | |
counter = counter + 1; | |
} | |
} | |
public int Get() | |
{ | |
// same as lock (lock is just a syntax sugar) | |
Monitor.Enter(mutex); | |
try | |
{ | |
return counter; | |
} | |
finally | |
{ | |
Monitor.Exit(mutex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment