This is a demonstration of a few runs of my instructor solution for the Fall 2018 Operating Systems "Threaded bars" programming assignment. The exercise is focused on using the basic POSIX thread functions pthread_create, pthread_join, pthread_cancel and pthread_exit.
This programs has a main thread (the main program is the original thread of execution... our code doesn't have to create a thread for it) that creates 26 threads to manage letter counters and a presenter thread that visualizes the letter counts.
Each letter managing thread randomly adds -1, 0 or 1 to the associated letter's count, constraining the count to [0, 70]. Each thread performs this operation at around 100Hz... "usleep(10000)" should do the trick. Each time, each thread randomly decides whether to exit with a 1/500 chance.
The presenter thread prints each letter, capitalized, the same number of times as its associated count on the same line before moving on to the next letter. If a letter's thread has randomly exited, the presenter renders the count with period characters instead of the original letter. This visualization happens at around 10 Hz... "usleep(100000)" should do the trick. Before each round of visualization, this thread should print 10 or so newlines to allow for a persistence of vision animation effect.
Once the main thread detects that each of the 26 letter threads has exited, it can cancel and join the presenter thread before exiting the program.