public void TestMultithreadedCorrectness() { int numOfIterations = 1000000; Parallel.For(0, Environment.ProcessorCount, j => { CounterStatistic.SetOrleansManagedThread(); for (int i = 0; i < counters.Length; i++) { counters[i] = CounterStatistic.FindOrCreate(new StatisticName("test" + i)); for (int k = 0; k < numOfIterations; k++) { counters[i].IncrementBy(i); } } }); for (int i = 0; i < counters.Length; i++) { var counter = CounterStatistic.FindOrCreate(new StatisticName("test" + i)); output.WriteLine("" + counter.GetCurrentValue()); Assert.AreEqual(i * Environment.ProcessorCount * numOfIterations, counter.GetCurrentValue()); } }
public void QueueWorkItem(WaitCallback callback, object state = null) { if (callback == null) { throw new ArgumentNullException(nameof(callback)); } new Thread(() => { // todo: statistic should be injected as dependency CounterStatistic.SetOrleansManagedThread(); // must be called before using CounterStatistic. try { TrackExecutionStart(); callback.Invoke(state); } catch (Exception exc) { HandleExecutionException(exc); } finally { TrackExecutionStop(); } }) { IsBackground = true, Name = Name }.Start(); }