fork(1) download
  1. using System;
  2. using System.Threading;
  3.  
  4. public class Test
  5. {
  6. static Thread winner = null;
  7.  
  8. private static void MyFunc()
  9. {
  10. Thread.Sleep((int)(new Random().NextDouble() * 1000));
  11. Interlocked.CompareExchange(ref winner, Thread.CurrentThread, null);
  12. }
  13.  
  14. public static void Main()
  15. {
  16. Thread thread1 = new Thread(() => MyFunc());
  17. Thread thread2 = new Thread(() => MyFunc());
  18. Thread thread3 = new Thread(() => MyFunc());
  19.  
  20. thread1.Name = "thread1";
  21. thread2.Name = "thread2";
  22. thread3.Name = "thread3";
  23.  
  24. thread1.Start();
  25. thread2.Start();
  26. thread3.Start();
  27.  
  28. thread1.Join();
  29. thread2.Join();
  30. thread3.Join();
  31.  
  32. Console.WriteLine("The winner is {0}", winner.Name);
  33. }
  34. }
Success #stdin #stdout 0.03s 36872KB
stdin
Standard input is empty
stdout
The winner is thread2