The purpose of this lab is to give you practice with threads in C++.
There are two programs for you to work on. The first one won't run so well in Visual Studio so you should run it on uaa-transformer.duckdns.org. All the files are located in
~kjmock/CSCE_A211/lab13. You can copy the files from that folder into your directory and edit them there.
The first program is here: counter.cpp
It creates 30,000 threads and each thread increments a counter. If the counter starts at 0 then this means that one would expect the final value of the counter to be 30,000. However, if you run it multiple times, then you will see that sometimes the counter value is less than 30,000. When compiling on Linux don't forget that you need to add the
-std=c++11 flag and also the
-lpthread flag, e.g:
g++ counter.cpp -std=c++11 -lpthread
The second program is here:
witty.cpp or you can use this as a Visual Studio version: wittyVS.cpp
It reads in this text file which should be in the default directory: witticism.txt
If you compile and run the program it will spit out a witty one liner every 5 seconds. The only way to stop the program is to hit control-c or kill the window. We would like it so if we press a key and enter, then to make the program stop.To do:
Witticismclass in a thread. This means moving the
showOneLiners()function into an
operator()()function, then modify
mainso it runs the class in a thread.
The program should now quit after typing a key (like 'q') and hitting enter.
Show your working code to the instructor or lab assistant for credit, or email your solution by midnight.