-
Notifications
You must be signed in to change notification settings - Fork 7
/
Semaphore.linq
39 lines (37 loc) · 956 Bytes
/
Semaphore.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<Query Kind="Program" />
//This is a sample usage of a Semaphore class to control access to lockeable resources by N entities at the same time, while allowing read access to M entities.
void Main()
{
SemaphoreUsage.Execute();
}
public static class SemaphoreUsage
{
public static readonly Semaphore semaphore = new(2, 4);
public static void Execute()
{
for (var cnt = 0; cnt < 6; cnt++)
{
Thread thread = new(DoWork)
{
Name = "Thread " + cnt
};
thread.Start();
}
}
private static void DoWork()
{
try
{
Console.WriteLine($"Thread {Thread.CurrentThread.Name} waits the lock");
semaphore.WaitOne();
Console.WriteLine($"Thread {Thread.CurrentThread.Name} enters critical section");
Thread.Sleep(500);
Console.WriteLine($"Thread {Thread.CurrentThread.Name} exits critical section");
}
finally
{
Console.WriteLine($"Thread {Thread.CurrentThread.Name} releases the lock");
semaphore.Release();
}
}
}