2 using System
.Threading
;
7 static readonly Mutex
[] mutexes
= new Mutex
[2];
9 public static void Main(string[] args
)
11 for (int i
= 0; i
< mutexes
.Length
; i
++) {
12 mutexes
[i
] = new Mutex();
15 Thread thread1
= new Thread(() => {
16 for (int i
= 0; i
< 1; i
++) {
19 idx
= WaitHandle
.WaitAny (mutexes
);
20 Console
.WriteLine($"Thread 1 iter: {i} with mutex: {idx}");
23 mutexes
[idx
].ReleaseMutex();
27 Console
.WriteLine("Thread 1 ended");
33 Thread thread2
= new Thread(() => {
34 for (int i
= 0; i
< 1000; i
++) {
37 idx
= WaitHandle
.WaitAny (mutexes
);
38 Console
.WriteLine($"Thread 2 iter: {i} with mutex: {idx}");
41 mutexes
[idx
].ReleaseMutex();
45 Console
.WriteLine("Thread 2 ended");