Created
November 19, 2017 19:21
-
-
Save luisdeol/a87f61451bd85b0336bee7fbee7aa681 to your computer and use it in GitHub Desktop.
Using the Interlocked Class
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++) | |
| 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