public void PerformSomeOperationWithNoLockTest() { const int counter = 40; var doneEvents = new ManualResetEvent[counter]; IList <InputOutputValue> inputOutputValues = new List <InputOutputValue>(); for (var loopCounter = 0; loopCounter < counter; loopCounter++) { doneEvents[loopCounter] = new ManualResetEvent(false); var inputOutputValue = new InputOutputValue { Input = loopCounter }; var helper = new ThreadPoolHelper(doneEvents[loopCounter]); ThreadPool.QueueUserWorkItem(helper.ThreadPoolCallbackWithNoLock, inputOutputValue); inputOutputValues.Add(inputOutputValue); } WaitHandle.WaitAll(doneEvents); var inputOutputValueCounter = 0; foreach (InputOutputValue inputOutputValue in inputOutputValues) { if (inputOutputValue.Input == inputOutputValue.Output) { inputOutputValueCounter++; } } Assert.NotEqual(counter, inputOutputValueCounter); }
public void PerformSomeOperationWithLock(InputOutputValue inputOutputValues) { lock (this) { State = inputOutputValues.Input; Thread.Sleep(1000); inputOutputValues.Output = State; } }