Merge pull request #1896 from meum/patch-1
[mono.git] / mcs / class / corlib / Test / System / LazyTest.cs
index ce75180cb1b31e8c2bbc3887625a060566463b2a..0b3e042e3097b6021ea395e5e2c7987f5c07f222 100644 (file)
@@ -114,6 +114,7 @@ namespace MonoTests.System
                public void EnsureSingleThreadSafeExecution ()
                {
                        counter = 42;
+                       bool started = false;
 
                        var l = new Lazy<int> (delegate () { return counter ++; }, true);
                        bool failed = false;
@@ -122,16 +123,20 @@ namespace MonoTests.System
                        for (int i = 0; i < threads.Length; ++i) {
                                threads [i] = new Thread (delegate () {
                                                lock (monitor) {
-                                                       if (!Monitor.Wait (monitor, 2000))
-                                                               failed = true;
+                                                       if (!started) {
+                                                               if (!Monitor.Wait (monitor, 2000))
+                                                                       failed = true;
+                                                       }
                                                }
                                                int val = l.Value;
                                        });
                        }
                        for (int i = 0; i < threads.Length; ++i)
                                threads [i].Start ();
-                       lock (monitor)
+                       lock (monitor) {
+                               started = true;
                                Monitor.PulseAll (monitor);
+                       }
 
                        for (int i = 0; i < threads.Length; ++i)
                                threads [i].Join ();