[System] Tweak socket test
[mono-project.git] / mono / tests / mutexes.cs
blob94af84160cf7335caa4b75b18cb98f9f8f881095
1 /////////////////////////////////// Test Overview ////////////////////////////////
2 //
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
5 // locks.
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 /////////////////////////////////////////////////////////////////////////////////
15 using System;
16 using System.Threading;
18 class MutexTest
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+"...");
30 m[i].WaitOne();
31 Console.WriteLine("[Thread A] - m["+i+"] Locked!");
32 Console.WriteLine("[Thread A] - Now using mutex ["+i+"]");
33 Thread.Sleep(2000);
34 m[i].ReleaseMutex();
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++)
48 int i=5;
49 Console.WriteLine("[Thread B] - Trying to lock mutex "+i+" for "+h+" time...");
50 m[i].WaitOne();
51 Console.WriteLine("[Thread B] - m["+i+"] Locked recursively ["+h+"] times!");
52 Thread.Sleep(4500);
54 for (int h=0;h<10;h++)
56 int i=5;
57 m[i].ReleaseMutex();
58 Console.WriteLine("[Thread B] - Unlocked the mutex ["+i+"] for ["+h+"] times");
61 Console.WriteLine("[Thread B] - Finished.....");
65 public static void Main()
67 m = new Mutex[10];
68 for (int i = 0 ; i<10 ; i++ )
69 m[i] = new Mutex();
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);
81 A.Start();
82 B.Start();
84 Thread.Sleep(500);
85 Console.WriteLine("[ Main ] - Test Ended");