Please complete the required studio exercises listed below, along with the (optional) enrichment exercises if they interest you.
As you work through the exercises, please record your answers in a file, and upon completion please send your answers in an e-mail with Half-Sync/Half-Async and Leader/Followers studio in the subject line, to the firstname.lastname@example.org course e-mail account.
Please make sure that the name of each person who worked on these exercises is listed at the top of your reply message, and that you number your answers so they are easy for us to match up with the appropriate exercise.
Write a simple function that takes an unsigned integer and returns a boolean result that is true if the value that was passed is a prime number, or false if it is not.
In the main C++ source code file for the project (which should be named something like
hsha_lf.cpp) please modify the main function signature so that it
looks like the standard (i.e., portable between Windows and Linux) main function entry point
int main (int, char * )
In your main function, call the function you wrote with different values, some of which are prime and some of which are not, and for each of them print out a message saying what the number was and whether or not it is prime.
Build and run your program, and as the answer to this exercise, please show your code and the output your program produced.
Using code from one or more of the previous studios, implement a thread pool (with at least several threads, though you may want to experiment with varying the number of threads) whose threads each can repeatedly (1) take an unsigned integer, (2) use the function you wrote to determine whether or not the number is prime, and (3) use the synchronization wrapper to print out (in a thread safe manner) to the standard output stream a message with the number and whether or not it is prime (hint: an input string stream can make it easy to convert the number and a string into a string).
Modify your main program so that for each of the unsigned integers, instead of calling the function and printing out the message it hands off the number to a waiting thread from the pool, which then performs the work in parallel with the other threads in the pool which are handling other numbers. Build and run your program, and as the answer to this exercise, please show your code and the output your program produced.
Note that this may require you to modify how threads are ordered to wait for the next number, i.e., each thread will become the leader when it first asks for a number and will continue to be the leader until it gets an odd number greater than 2, before any other thread should be able to become the leader.
In your main function, hand off many numbers to the thread pool, of varying sizes and kinds (some even, some odd, some large, some small), so that the leader role is exercised to a reasonable extent by each thread that becomes the leader but leaders also change reasonably often.
Build and run your program, and as the answer to this exercise, please show your code and a representative sample of the output your program produced which indicates.
cin), and then immediately hands each one off to the thread pool.
Build and run your program, and try running it both by typing numbers in one (or a few) at a time in the
terminal window (hitting enter
feeds the line to
cin) and by streaming a file (e.g., populated with the numbers that main used in the
previous exercise) to the program's standard input. As the answer to this exercise, please describe what
you saw in terms of (1) how responsive the program was when you typed in the numbers manually, and (2) how
rapid the program's throughput was when you streamed the numbers from a file, both relative to each other and
to what you observed in the previous exercise.