Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / sgen-new-threads-dont-join-stw-2.cs
1
2 using System;
3 using System.Collections.Concurrent;
4 using System.Collections.Generic;
5 using System.Threading;
6
7 class Driver
8 {
9         public static void Main ()
10         {
11                 BlockingCollection<Thread> threads = new BlockingCollection<Thread> (128);
12
13                 Thread producer = new Thread (new ThreadStart (() => {
14                         DateTime start = DateTime.Now;
15
16                         for (TestTimeout timeout = TestTimeout.Start(TimeSpan.FromSeconds(TestTimeout.IsStressTest ? 120 : 5)); timeout.HaveTimeLeft;) {
17                                 Thread worker = new Thread (new ThreadStart (() => {
18                                         HashSet<string> hashset = new HashSet<string> ();
19                                         for (int i = 0; i < 50000; ++i) {
20                                                 hashset.Add(string.Concat (i, i));
21                                                 if (i % 10 == 0)
22                                                         Thread.Yield ();
23                                         }
24                                 }));
25
26                                 worker.Start ();
27
28                                 threads.Add (worker);
29
30                                 Console.WriteLine ("Started thread {0} ({1} running concurrently)", worker.ManagedThreadId, threads.Count);
31                         }
32
33                         threads.CompleteAdding ();
34                 }));
35
36                 Thread consumer = new Thread (new ThreadStart(() => {
37                         while (!threads.IsCompleted) {
38                                 Thread worker = threads.Take ();
39                                 worker.Join ();
40                                 Console.WriteLine ("Joined thread {0}", worker.ManagedThreadId);
41                         }
42                 }));
43
44                 producer.Start ();
45                 consumer.Start ();
46
47                 producer.Join ();
48                 consumer.Join ();
49         }
50 }