/* * Homework #3 * * For this assignment, you will alter this program to make it thread-safe. * Use whatever approach you prefer. * * The current code is not thread safe. If you run it without modifications, it will probably crash. * * A successful homework will produce the correct number of primes between * one million and one hundred million and make use of all of the processor * cores on the computer. A single threaded solution is not acceptable. * * To run the program, you will first need to create an input data file. To do so, run * this program like this: * * Homework3.exe --createDataFile numbers.txt * * To run the computation, run the program like this: * * Homework3.exe --processDataFile numbers.txt * */ static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException; if (args.Length == 2) { var fileName = new FileInfo(args[1]); if (args[0] == "--createDataFile") { using (var writer = new NumberWriter(fileName)) { writer.WriteIntegers(Sequence.Create(Constants.LowerBound, Constants.UpperBound)); //Constants.LowerBound, .UpperBound } } else if (args[0] == "--processDataFile") { var startTime = DateTime.Now; using (var reader = new NumberReader(fileName)) { new Calculator().Run(reader); } var totalSeconds = (DateTime.Now - startTime).TotalSeconds; Console.WriteLine("Program took {0} seconds to run", totalSeconds); Console.ReadLine(); } else { PrintUsage(); } } else { PrintUsage(); } }
public void Run(NumberReader reader) { lock (synclock) { var results = new List<long>(); var numbersToCheck = new Queue<long>(); StartComputationThreads(results, numbersToCheck); var progressMonitor = new ProgressMonitor(results); new Thread(progressMonitor.Run) { IsBackground = true }.Start(); foreach (var value in reader.ReadIntegers()) { numbersToCheck.Enqueue(value); } while (numbersToCheck.Count > 0) { Thread.Sleep(100); // wait for the computation to complete. } Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount); } }
public void Run(NumberReader reader) { var results = new List<long>(); var numbersToCheck = new Queue<long>(); StartComputationThreads(results, numbersToCheck); var progressMonitor = new ProgressMonitor(results); new Thread(progressMonitor.Run) {IsBackground = true}.Start(); reader.ReadIntegers(numbersToCheck); //foreach (var value in reader.ReadIntegers()) //{ // lock (IsNumberPrimeCalculator.LockQueue) // { // numbersToCheck.Enqueue(value); // } //} while (true) { bool keepGoing = true; lock (IsNumberPrimeCalculator.LockQueue) { keepGoing = numbersToCheck.Count > 0; } if (keepGoing == false) { break; } Thread.Sleep(100); // wait for the computation to complete. } Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount); }
public void Run(NumberReader reader) { //Start Threads. StartComputationThreads(CalculatorboundBuffer.getResultList(), CalculatorboundBuffer.getQueueForNumbersToCheck()); //Start Progress Monitor. var progressMonitor = new ProgressMonitor(CalculatorboundBuffer.getResultList()); //Run background threads. new Thread(progressMonitor.Run) { IsBackground = true }.Start(); //Read in numbers from notepad. reader.ReadIntegers(CalculatorboundBuffer.getQueueForNumbersToCheck()); while (CalculatorboundBuffer.getQueueForNumbersToCheck().Count > 0) { Thread.Sleep(100); } Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount); }
/* * Homework #3 * * For this assignment, you will alter this program to make it thread-safe. * Use whatever approach you prefer. * * The current code is not thread safe. If you run it without modifications, it will probably crash. * * A successful homework will produce the correct number of primes between * one million and one hundred million and make use of all of the processor * cores on the computer. A single threaded solution is not acceptable. * * To run the program, you will first need to create an input data file. To do so, run * this program like this: * * Homework3.exe --createDataFile numbers.txt * * To run the computation, run the program like this: * * Homework3.exe --processDataFile numbers.txt * */ /* public void lockThis(int name) * { * lock (thisLock) * { * * * //System.Threading.Thread.Sleep(10000); * } * }*/ static void Main(string[] args) { //lock (uuLock) //{ AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException; if (args.Length == 2) { var fileName = new FileInfo(args[1]); /*NumberWriter numwrite = new NumberWriter(fileName); * Thread lolThread = new Thread(new ThreadStart(numwrite.NumberWriter));*/ if (args[0] == "--createDataFile") { using (var writer = new NumberWriter(fileName)) { writer.WriteIntegers(Sequence.Create(Constants.LowerBound, Constants.UpperBound)); } } else if (args[0] == "--processDataFile") { var startTime = DateTime.Now; using (var reader = new NumberReader(fileName)) { new Calculator().Run(reader); } var totalSeconds = (DateTime.Now - startTime).TotalSeconds; Console.WriteLine("Program took {0} seconds to run", totalSeconds); Console.ReadLine(); } else { PrintUsage(); } } else { PrintUsage(); } //} Console.WriteLine("Hi"); }
public void Run(NumberReader reader) { BoundBuff = new BoundBuffer<long>(); StartComputationThreads(BoundBuff.GetList(), BoundBuff.GetQueue()); var progressMonitor = new ProgressMonitor(BoundBuff.GetList()); new Thread(progressMonitor.Run) {IsBackground = true}.Start(); foreach (var value in reader.ReadIntegers()) { BoundBuff.Enqueue(value); } while (BoundBuff.GetQueue().Count > 0) { Thread.Sleep(100); // wait for the computation to complete. } Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount); }
public void Run(NumberReader reader) { var results = new List <long>(); var numbersToCheck = new BoundBuffer <long>(); StartComputationThreads(results, numbersToCheck); var progressMonitor = new ProgressMonitor(results); new Thread(progressMonitor.Run) { IsBackground = true }.Start(); foreach (var value in reader.ReadIntegers()) { numbersToCheck.Enqueue(value); } while (numbersToCheck.Count() > 0) { Thread.Sleep(100); } Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount); }
public void Run(NumberReader reader) { var results = new List <long>(); var numbersToCheck = new Queue <long>(); StartComputationThreads(results, numbersToCheck); var progressMonitor = new ProgressMonitor(results); new Thread(progressMonitor.Run) { IsBackground = true }.Start(); reader.ReadIntegers(numbersToCheck); //foreach (var value in reader.ReadIntegers()) //{ // lock (IsNumberPrimeCalculator.LockQueue) // { // numbersToCheck.Enqueue(value); // } //} while (true) { bool keepGoing = true; lock (IsNumberPrimeCalculator.LockQueue) { keepGoing = numbersToCheck.Count > 0; } if (keepGoing == false) { break; } Thread.Sleep(100); // wait for the computation to complete. } Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount); }