Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ConsoleApp23
- {
- class Program
- {
- public static Semaphore semaphore = new Semaphore(3, 5);
- public static void Main(string[] args)
- {
- for (int i = 0; i < 10; i++)
- {
- Thread threadObject = new Thread(Process);
- threadObject.Name = "Thread: " + i;
- threadObject.Start();
- }
- Console.ReadLine();
- }
- private static void Process()
- {
- Console.WriteLine("{0} is waiting to enter the critical section.", Thread.CurrentThread.Name);
- semaphore.WaitOne();
- Console.WriteLine("{0} is inside the critical section now...", Thread.CurrentThread.Name);
- Thread.Sleep(1000);
- Console.WriteLine("{0} is releasing the critical section...", Thread.CurrentThread.Name);
- semaphore.Release();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement