Skip to content

Instantly share code, notes, and snippets.

@mirasrael
Last active April 15, 2022 09:09
Show Gist options
  • Save mirasrael/260d851f32d14e5a0d07800f338fca37 to your computer and use it in GitHub Desktop.
Save mirasrael/260d851f32d14e5a0d07800f338fca37 to your computer and use it in GitHub Desktop.
Mutex
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