Apply changes from https://github.com/dotnet/runtime/commit/eb1756e97d23df13bc6fe798e...
[mono-project.git] / mono / tests / thread-suspend-suspended.cs
blob16fad87188226ba665bfb8ccaf175c3572ec138f
2 using System;
3 using System.Runtime.CompilerServices;
4 using System.Threading;
6 class Driver
9 public static void Main ()
11 bool finished = false;
12 AutoResetEvent start_gc = new AutoResetEvent (false);
13 AutoResetEvent finished_gc = new AutoResetEvent (false);
15 Thread t1 = new Thread (() => {
16 while (!Volatile.Read(ref finished)) {}
17 });
19 Thread t2 = new Thread (() => {
20 while (!Volatile.Read(ref finished)) {
21 if (start_gc.WaitOne (0)) {
22 GC.Collect ();
23 finished_gc.Set ();
26 Thread.Yield ();
28 });
30 t1.Start ();
31 t2.Start ();
33 Thread.Sleep (10);
35 for (int i = 0; i < 50 * 40 * 5; ++i) {
36 t1.Suspend ();
37 start_gc.Set ();
38 finished_gc.WaitOne ();
39 t1.Resume ();
41 if ((i + 1) % (50) == 0)
42 Console.Write (".");
43 if ((i + 1) % (50 * 40) == 0)
44 Console.WriteLine ();
47 Volatile.Write(ref finished, true);
49 t1.Join ();
50 t2.Join ();