Merge pull request #5396 from kumpera/fix_11696
[mono.git] / mono / tests / thread3.cs
1
2 using System;
3 using System.Threading;
4
5 public class Test {
6         private void Thread_func() {
7                 Console.WriteLine("In a thread!");
8                 
9                 Thread thr=Thread.CurrentThread;
10                 
11                 Console.WriteLine("Locking thr for 1.5s");
12                 lock(thr) {
13                         Console.WriteLine("Locked");
14                         Thread.Sleep(2000);
15                         Console.WriteLine("Slept for 2s");
16                         Thread.Sleep(15000);
17                 }
18                 
19                 Console.WriteLine("Waiting for signal");
20                 lock(thr) {
21                         Console.WriteLine("Waiting...");
22                         Monitor.Wait(thr);
23                         Console.WriteLine("Thread signalled!");
24                 }
25                 
26                 Console.WriteLine("Sleeping for 2s");
27                 Thread.Sleep(2000);
28
29                 Console.WriteLine("Leaving thread");
30         }
31         
32         public static int Main () {
33                 Console.WriteLine ("Hello, World!");
34                 Thread thr=new Thread(new ThreadStart(new Test().Thread_func));
35                 thr.Start();
36                 Thread.Sleep(1000);
37
38                 Console.WriteLine("Trying to enter lock");
39                 if(Monitor.TryEnter(thr, 1000)==true) {
40                         Console.WriteLine("Returned lock");
41                         Monitor.Exit(thr);
42                 } else {
43                         Console.WriteLine("Didn't get lock");
44                         // .net seems to leave thr locked here !!!!
45                         
46                         // This test deadlocks on .net with the thread
47                         // trying to lock(thr) between the two
48                         // WriteLine()s Monitor.Exit(thr); here and it
49                         // magically works :) Of course, then mint
50                         // throws a
51                         // SynchronizationLockException... (like it
52                         // should)
53                         //Monitor.Exit(thr);
54                 }
55                 
56                 Thread.Sleep(20000);
57
58                 lock(thr) {
59                         Monitor.Pulse(thr);
60                         Console.WriteLine("Signalled thread");
61                 }
62                 
63                 thr.Join();
64                 
65                 return 0;
66         }
67 }
68