public static WaitForConditionWithCustomDelay ( Func |
||
condition | Func |
|
delay | Action | |
return | void |
public static void RunThreadLocalTest5_Dispose() { // test recycling the combination index; ThreadLocal <string> tl = new ThreadLocal <string>(() => null); Assert.False(tl.IsValueCreated); Assert.Null(tl.Value); // Test that a value is not kept alive by a departed thread var threadLocal = new ThreadLocal <SetMreOnFinalize>(); var mres = new ManualResetEventSlim(false); // (Careful when editing this test: saving the created thread into a local variable would likely break the // test in Debug build.) // We are creating the task using TaskCreationOptions.LongRunning because... // there is no guarantee that the Task will be created on another thread. // There is also no guarantee that using this TaskCreationOption will force // it to be run on another thread. new Task(() => { threadLocal.Value = new SetMreOnFinalize(mres); }, TaskCreationOptions.LongRunning).Start(TaskScheduler.Default); ThreadTestHelpers.WaitForConditionWithCustomDelay( () => mres.IsSet, () => { Thread.Sleep(1); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); }); }
public void InterlockedAddAndRead_Multithreaded_Int64() { const int ThreadCount = 10; const int IterationCount = 100; const long Increment = ((long)1 << 32) + 1; long value = 0; var threadStarted = new AutoResetEvent(false); var startTest = new ManualResetEvent(false); int completedThreadCount = 0; Action threadStart = () => { threadStarted.Set(); startTest.CheckedWait(); for (int i = 0; i < IterationCount; ++i) { Interlocked.Add(ref value, Increment); } Interlocked.Increment(ref completedThreadCount); }; var checksForThreadErrors = new Action[ThreadCount]; var waitsForThread = new Action[ThreadCount]; for (int i = 0; i < ThreadCount; ++i) { Thread t = ThreadTestHelpers.CreateGuardedThread(out checksForThreadErrors[i], out waitsForThread[i], threadStart); t.IsBackground = true; t.Start(); threadStarted.CheckedWait(); } startTest.Set(); ThreadTestHelpers.WaitForConditionWithCustomDelay( () => completedThreadCount >= ThreadCount, () => { long valueSnapshot = Interlocked.Read(ref value); Assert.Equal((int)valueSnapshot, (int)(valueSnapshot >> 32)); foreach (var checkForThreadErrors in checksForThreadErrors) { checkForThreadErrors(); } Thread.Sleep(1); }); foreach (var waitForThread in waitsForThread) { waitForThread(); } Assert.Equal(ThreadCount, completedThreadCount); Assert.Equal(ThreadCount * IterationCount * Increment, Interlocked.Read(ref value)); }