TheBulgarianWolf

ThreadSafeExample

Nov 20th, 2020
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.55 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace MoreMethodExercises
  5. {
  6.     class Program
  7.     {
  8.         static bool done;
  9.         static readonly object locker = new object();
  10.         static void Main(string[] args)
  11.         {
  12.             new Thread(Go).Start();
  13.             Go();
  14.         }
  15.  
  16.         static void Go()
  17.         {
  18.             lock (locker)
  19.             {
  20.                 if (!done)
  21.                 {
  22.                     Console.WriteLine("Done");
  23.                     done = true;
  24.                 }
  25.             }
  26.         }
  27.     }
  28. }
Add Comment
Please, Sign In to add comment