Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Threading;
- namespace Semaphore_Demo
- {
- class Program
- {
- static Semaphore semaphore = new Semaphore(3, 3);
- static void Main(string[] args)
- {
- for (int i = 0; i < 20; i++)
- {
- new Thread(DoSomething).Start(i);
- }
- Console.Read();
- }
- static void DoSomething(object id)
- {
- Console.WriteLine(id + " wants to access the semaphore");
- semaphore.WaitOne();
- Console.WriteLine(id + " as successed to access the semaphore");
- Thread.Sleep(2000);
- Console.WriteLine(id + " is about to leave the smeaphore");
- semaphore.Release();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement