/
mutexes.cs
87 lines (72 loc) · 2.61 KB
/
mutexes.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/////////////////////////////////// Test Overview ////////////////////////////////
//
// Two threads are started from Main, which allocates 10 static mutexes.
// The first thread locks each mutex in turn, with a delay of 2000ms between
// locks.
// The second thread recursively locks mutex no. 5 10 times, blocking the
// progress of the first thread as this second thread has a delay of 4500ms
// between each lock.When the second thread has called ReleaseMutex on the mutex 10
// times it terminates and the first thread can carry on its cycle of locking and
// releasing mutexes until it exits.
//
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Threading;
class MutexTest
{
public static Mutex[] m;
// Code for first thread
public static void ThreadMethod_A()
{
Console.WriteLine("[Thread A] - Started.....");
for (int i=0;i<10;i++)
{
Console.WriteLine("[Thread A] - Trying to lock mutex "+i+"...");
m[i].WaitOne();
Console.WriteLine("[Thread A] - m["+i+"] Locked!");
Console.WriteLine("[Thread A] - Now using mutex ["+i+"]");
Thread.Sleep(2000);
m[i].ReleaseMutex();
Console.WriteLine("[Thread A] - Unlocked the mutex ["+i+"]");
}
Console.WriteLine("[Thread A] - exiting.....");
}
// Code for second thread
public static void ThreadMethod_B()
{
Console.WriteLine("[Thread B] - Started.....");
for (int h=0;h<10;h++)
{
int i=5;
Console.WriteLine("[Thread B] - Trying to lock mutex "+i+" for "+h+" time...");
m[i].WaitOne();
Console.WriteLine("[Thread B] - m["+i+"] Locked recursively ["+h+"] times!");
Thread.Sleep(4500);
}
for (int h=0;h<10;h++)
{
int i=5;
m[i].ReleaseMutex();
Console.WriteLine("[Thread B] - Unlocked the mutex ["+i+"] for ["+h+"] times");
}
Console.WriteLine("[Thread B] - Finished.....");
}
public static void Main()
{
m = new Mutex[10];
for (int i = 0 ; i<10 ; i++ )
m[i] = new Mutex();
// Create the first thread
Console.WriteLine("[ Main ] - Creating first thread..");
ThreadStart Thread_1 = new ThreadStart(ThreadMethod_A);
// Create the second thread
Console.WriteLine("[ Main ] - Creating second thread..");
ThreadStart Thread_2 = new ThreadStart(ThreadMethod_B);
Thread A = new Thread(Thread_1);
Thread B = new Thread(Thread_2);
A.Start();
B.Start();
Thread.Sleep(500);
Console.WriteLine("[ Main ] - Test Ended");
}
}