Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created November 19, 2017 18:17
Show Gist options
  • Save luisdeol/eebce819f98f05cdf7ed18a480481fba to your computer and use it in GitHub Desktop.
Save luisdeol/eebce819f98f05cdf7ed18a480481fba to your computer and use it in GitHub Desktop.
Using the lock operator
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++)
lock(_lockTheSavior)
theAlmightyZero++;
});
for (int j = 0; j < 1000000; j++)
lock(_lockTheSavior)
theAlmightyZero--;
newAmazingTask.Wait();
Console.WriteLine("Here it comes a ZERO value (now that is true)! "+theAlmightyZero);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment