Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created November 19, 2017 19:21
Show Gist options
  • Select an option

  • Save luisdeol/a87f61451bd85b0336bee7fbee7aa681 to your computer and use it in GitHub Desktop.

Select an option

Save luisdeol/a87f61451bd85b0336bee7fbee7aa681 to your computer and use it in GitHub Desktop.
Using the Interlocked Class
namespace manage_multithreading
{
class Program
{
static void Main(string[] args)
{
int theAlmightyZero = 0;
object _lockTheSavior = new object();
Task newAmazingTask = Task.Run(() =>
{
for (int i = 0; i < 1000000; i++)
Interlocked.Increment(ref theAlmightyZero);
});
for (int j = 0; j < 1000000; j++)
Interlocked.Decrement(ref theAlmightyZero);
newAmazingTask.Wait();
Console.WriteLine("Here it comes a ZERO value (that is STILL true)! " + theAlmightyZero);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment