Пример #1
0
        public void IsThreadSafe()
        {
            const int ThreadCount = 10;

            // Start source threads.
            SourceThread[] sources = new SourceThread[ThreadCount];
            for (int i = 0; i < ThreadCount; i++)
            {
                sources[i] = new SourceThread();
                sources[i].Start();
            }

            // Start target threads.
            TargetThread[] targets = new TargetThread[ThreadCount];
            for (int i = 0; i < ThreadCount; i++)
            {
                targets[i] = new TargetThread(sources);
                targets[i].Start();
            }

            // Wait for all threads to finish.
            for (int i = 0; i < ThreadCount; i++)
            {
                sources[i].Join();
                targets[i].Join();
            }

            // All targets are in the correct state.
            for (int i = 0; i < ThreadCount; i++)
            {
                Assert.AreEqual(ThreadCount * SourceThread.MaxValue, targets[i].Total);
            }
        }
Пример #2
0
 public TargetThread(SourceThread[] sources)
 {
     _sources = sources;
     _thread = new Thread(ThreadProc);
     _total = new Dependent<int>(() => _sources.Sum(source => source.Value));
 }