Advertisement
ivorakitin

Barrier

Apr 9th, 2025 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp24
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             /*
  15.             Console.WriteLine("Countdown example");
  16.             for (int i = 0; i < CountDownEventExample.numEvents i++)
  17.             {
  18.                 //Console.WriteLine("Press <Enter>");
  19.                 //Console.ReadLine();
  20.                 Thread t = new Thread(CountDownEventExample.MyMethod);
  21.                 t.Name = "Thread " + i;
  22.                 t.Start();
  23.             }
  24.             CountDownEventExample.countDownEvent.Wait();
  25.             Console.WriteLine("Unblocked");
  26.             Console.ReadLine();*/
  27.             /*
  28.             Console.WriteLine("Barrier example");
  29.             Thread t1 = new Thread(BarrierExample.MyMethod);
  30.             t1.Name = "1";
  31.             Thread t2 = new Thread(BarrierExample.MyMethod);
  32.             t2.Name = "2";
  33.             t1.Start();
  34.             t2.Start();*/
  35.             Thread[] threads = new Thread[5];
  36.             for (int i = 0; i < threads.Length; i++)
  37.             {
  38.                 threads[i] = new Thread(BarrierExample2.MyMethod);
  39.                 threads[i].Name = "Thread " + i;
  40.             }
  41.             for (int i = 0; i < threads.Length; i++)
  42.             {
  43.                 threads[i].Start();
  44.             }
  45.             Console.ReadLine();
  46.         }
  47.     }
  48.  
  49.     class CountDownEventExample
  50.     {
  51.         public static int numEvents = 2;
  52.         public static CountdownEvent countDownEvent = new CountdownEvent(numEvents);
  53.  
  54.         public static void MyMethod()
  55.         {
  56.             for (int i = 0; i < 5; i++)
  57.             {
  58.                 Console.WriteLine(Thread.CurrentThread.Name + " " + i);
  59.                 Thread.Sleep(200);
  60.             }
  61.             countDownEvent.Signal();
  62.         }
  63.     }
  64.  
  65.     class BarrierExample
  66.     {
  67.         public static Barrier barrier = new Barrier(2); //2 threads
  68.        
  69.         public static void MyMethod()
  70.         {
  71.             for (int i = 0; i < 4; i++)
  72.             {
  73.                 Console.WriteLine("Thread {0} reached phase {1}",
  74.                     Thread.CurrentThread.Name, barrier.CurrentPhaseNumber);
  75.                 barrier.SignalAndWait();
  76.             }
  77.         }
  78.     }
  79.  
  80.     class BarrierExample2
  81.     {
  82.         public static Barrier barrier = new Barrier(5); //2 threads
  83.         public static void MyMethod()
  84.         {
  85.             Random r = new Random();
  86.      
  87.             for (int i = 0; i < 5; i++)
  88.             {
  89.                 Thread.Sleep(r.Next(1000));
  90.                 Console.WriteLine("Thread {0} reached phase {1}",
  91.                     Thread.CurrentThread.Name, barrier.CurrentPhaseNumber);
  92.                 barrier.SignalAndWait();
  93.             }
  94.         }
  95.     }
  96. }
  97.  
Tags: barrier
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement