1 /////////////////////////////////// Test Overview ////////////////////////////////
3 // Two threads are started from Main, which allocates 10 static mutexes.
4 // The first thread locks each mutex in turn, with a delay of 2000ms between
6 // The second thread recursively locks mutex no. 5 10 times, blocking the
7 // progress of the first thread as this second thread has a delay of 4500ms
8 // between each lock.When the second thread has called ReleaseMutex on the mutex 10
9 // times it terminates and the first thread can carry on its cycle of locking and
10 // releasing mutexes until it exits.
12 /////////////////////////////////////////////////////////////////////////////////
16 using System
.Threading
;
20 public static Mutex
[] m
;
22 // Code for first thread
23 public static void ThreadMethod_A()
25 Console
.WriteLine("[Thread A] - Started.....");
27 for (int i
=0;i
<10;i
++)
29 Console
.WriteLine("[Thread A] - Trying to lock mutex "+i
+"...");
31 Console
.WriteLine("[Thread A] - m["+i
+"] Locked!");
32 Console
.WriteLine("[Thread A] - Now using mutex ["+i
+"]");
35 Console
.WriteLine("[Thread A] - Unlocked the mutex ["+i
+"]");
38 Console
.WriteLine("[Thread A] - exiting.....");
41 // Code for second thread
42 public static void ThreadMethod_B()
44 Console
.WriteLine("[Thread B] - Started.....");
46 for (int h
=0;h
<10;h
++)
49 Console
.WriteLine("[Thread B] - Trying to lock mutex "+i
+" for "+h
+" time...");
51 Console
.WriteLine("[Thread B] - m["+i
+"] Locked recursively ["+h
+"] times!");
54 for (int h
=0;h
<10;h
++)
58 Console
.WriteLine("[Thread B] - Unlocked the mutex ["+i
+"] for ["+h
+"] times");
61 Console
.WriteLine("[Thread B] - Finished.....");
65 public static void Main()
68 for (int i
= 0 ; i
<10 ; i
++ )
71 // Create the first thread
72 Console
.WriteLine("[ Main ] - Creating first thread..");
73 ThreadStart Thread_1
= new ThreadStart(ThreadMethod_A
);
75 // Create the second thread
76 Console
.WriteLine("[ Main ] - Creating second thread..");
77 ThreadStart Thread_2
= new ThreadStart(ThreadMethod_B
);
79 Thread A
= new Thread(Thread_1
);
80 Thread B
= new Thread(Thread_2
);
85 Console
.WriteLine("[ Main ] - Test Ended");