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