예제 #1
0
        public void Run(NumberReader reader)
        {
            var results = new List<long>();
            var numbersToCheck = new BoundBuffer<long>();
            RwLock _wlock = new RwLock();

            StartComputationThreads(results, numbersToCheck);

            var progressMonitor = new ProgressMonitor(results);

            new Thread(progressMonitor.Run) {IsBackground = true}.Start();

            _wlock.WriteLock();
            foreach (var value in reader.ReadIntegers()) {
                numbersToCheck.Enqueue(value);                //critical section
            }

            while (numbersToCheck.Count() > 0)
            {
                Thread.Sleep(100); // wait for the computation to complete. New Lock?
            }

            Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount);
            _wlock.WriteUnLock();
        }