2007-12-06 Jb Evain <jbevain@novell.com>
[mono.git] / mono / tests / thread2.cs
blob5ae013887789ca0bfdd09ec26450fd2d82b5ae8b
2 using System;
3 using System.Threading;
5 public class Test {
6 static LocalDataStoreSlot slot;
8 private void Thread_func() {
9 //Throws undocumented exception :-(
10 //LocalDataStoreSlot namedslot=Thread.AllocateNamedDataSlot("data-slot");
11 LocalDataStoreSlot namedslot=Thread.GetNamedDataSlot("data-slot");
13 Console.WriteLine("In a thread!");
15 Thread thr=Thread.CurrentThread;
16 Console.WriteLine("Found thread!");
17 thr.Name="wobble";
18 Thread otherthr=Thread.CurrentThread;
19 Console.WriteLine("Other subthread is " + otherthr.Name);
21 Thread.SetData(slot, thr);
22 Thread storedthr=(Thread)Thread.GetData(slot);
23 Console.WriteLine("Stored subthread is " + storedthr.Name);
25 Thread.SetData(namedslot, thr);
26 storedthr=(Thread)Thread.GetData(namedslot);
27 Console.WriteLine("Stored subthread is " + storedthr.Name);
29 Console.WriteLine("Locking thr for 1.5s");
30 lock(thr) {
31 Thread.Sleep(1500);
34 Console.WriteLine("Waiting for signal");
35 lock(thr) {
36 Monitor.Wait(thr);
37 Console.WriteLine("Thread signalled!");
40 Console.WriteLine("Sleeping for 10s");
41 Thread.Sleep(10000);
43 Thread storedthr2=(Thread)Thread.GetData(slot);
44 Console.WriteLine("Stored subthread is still " + storedthr2.Name);
47 public static int Main () {
48 Console.WriteLine ("Hello, World!");
49 slot=Thread.AllocateDataSlot();
50 LocalDataStoreSlot namedslot=Thread.AllocateNamedDataSlot("data-slot");
52 Test test = new Test();
53 Thread thr=new Thread(new ThreadStart(test.Thread_func));
54 thr.Start();
55 Thread.Sleep(1000);
56 Thread main=Thread.CurrentThread;
57 main.Name="wibble";
58 Thread othermain=Thread.CurrentThread;
59 Console.WriteLine("Other name " + othermain.Name);
60 Thread.Sleep(0);
62 Console.WriteLine("In the main line!");
64 Console.WriteLine("Trying to enter lock");
65 if(Monitor.TryEnter(thr, 100)==true) {
66 Console.WriteLine("Returned lock");
67 Monitor.Exit(thr);
68 } else {
69 Console.WriteLine("Didn't get lock");
72 Thread.SetData(slot, main);
73 Thread storedthr=(Thread)Thread.GetData(slot);
74 Console.WriteLine("Stored subthread is " + storedthr.Name);
76 Thread.SetData(namedslot, main);
77 storedthr=(Thread)Thread.GetData(namedslot);
78 Console.WriteLine("Stored subthread is " + storedthr.Name);
80 if(thr.Join(5000)) {
81 Console.WriteLine("Joined thread");
82 } else {
83 Console.WriteLine("Didn't join thread");
86 lock(thr) {
87 Monitor.Pulse(thr);
88 Console.WriteLine("Signalled thread");
91 thr.Join();
93 return 0;