2010-04-19 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / thread-static.cs
blobdd8defd763c3b9df6fac47613b67bd52defb967f
1 using System;
2 using System.Threading;
3 using System.Reflection;
5 class T {
6 [ThreadStatic]
7 static int var = 5;
9 static bool tfailed = true;
11 static void thread () {
12 tfailed = false;
13 if (var != 0)
14 tfailed = true;
15 Console.WriteLine ("start value: {0}", var);
16 for (int i = 0; i < 10; ++i) {
17 var += 10;
18 Thread.Sleep (5);
20 Console.WriteLine ("end value: {0}", var);
21 if (var != 100)
22 tfailed = true;
25 static int Main () {
26 bool failed = false;
27 var = 10;
28 Thread thr = new Thread (new ThreadStart (thread));
29 thr.Start ();
30 if (var != 10)
31 failed = true;
32 var = 20;
33 Console.WriteLine ("value in main thread: {0}", var);
34 thr.Join ();
35 Console.WriteLine ("value in main thread after join: {0}", var);
36 if (var != 20)
37 failed = true;
39 if (failed)
40 return 1;
41 if (tfailed)
42 return 2;
44 /* Test access though reflection */
45 var = 42;
46 FieldInfo fi = typeof (T).GetField ("var", BindingFlags.NonPublic|BindingFlags.Static);
47 if ((int)fi.GetValue (null) != 42)
48 return 3;
49 fi.SetValue (null, 43);
50 if (var != 43)
51 return 4;
53 return 0;