Created
November 19, 2017 18:17
-
-
Save luisdeol/eebce819f98f05cdf7ed18a480481fba to your computer and use it in GitHub Desktop.
Using the lock operator
This file contains hidden or 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
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