Advertisement
mjc65

Example 1-35. Accessing shared data in a multithreaded appli

Jun 23rd, 2020
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.47 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3.  
  4. namespace Chapter1
  5. {
  6.     public class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int n = 0;
  11.  
  12.             var up = Task.Run(() =>
  13.             {
  14.                 for (int i = 0; i < 1000000; i++)
  15.                     n++;
  16.             });
  17.  
  18.             for (int i = 0; i < 1000000; i++)
  19.                 n--;
  20.            
  21.             up.Wait();
  22.             Console.WriteLine(n);
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement