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(); }
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(); }
public void WriteIntegers(IEnumerable <long> values) { _wlock.WriteLock(); foreach (var value in values) //critical section { _writer.WriteLine(value); } _wlock.WriteUnLock(); }
public void CheckIfNumbersArePrime() { while (true) { mutex.WriteLock(); var numberToCheck = _numbersToCheck.Dequeue(); //critical section if (IsNumberPrime(numberToCheck)) { _primeNumbers.Add(numberToCheck); } mutex.WriteUnLock(); } }
public void Run() { while (true) { Thread.Sleep(100); // wait for 1/10th of a second _lock.WriteLock(); var currentcount = _results.Count; //critical sections TotalCount += currentcount; _results.Clear(); // clear out the current primes to save some memory if (currentcount > 0) { Console.WriteLine("{0} primes found so far", TotalCount); } _lock.WriteUnLock(); } }