This third lab will explore how a race condition may occur when executing multiple threads in the same address space, as well as what affects the occurrence of a race condition.
This lab is an individual deliverable.
Hand in your final code and screenshots from the various runs used to answer the below questions, as well as the answers to those questions.
Email me your deliverable before midnight 11 October 2024.
You will need a c compiler as well as text editor/IDE to do this lab. If you need a bit of a refresher, our book authors provide a very nice introduction and guide here.
You will run a simple program which runs a bunch of threads and simulates a race condition. You will then explore the factors that affect it.
Read through the code so you can understand what it does. You will note that the main() section includes some commented out code exploring a deadlock. We have not covered this yet, so you can ignore it, for now.
The main() method has three sections, which are explained. Run the program once to see what it does.
Modify the program as follows, and then run it at each step (maybe a number of times), to help you answer the questions posed:
Add at least 5 additional threads to the code*. Does this impact the severity of the race condition? If so, how? Add yet some more threads and divine a trend. State the trend.
Add some computational load/complexity** to the print_hello function. Does this impact the severity of the race condition for the various thread scenarios you have built above? Why or why not?
Add some computational load/complexity** to the increment_counter function. Does this impact the severity of the race condition for the various thread scenarios you have built? Why or why not?
Add timing measurements to compare the performance of the locked vs. unlocked versions, and ensure the two versions are otherwise computationally equivalent. How does the performance of these two versions change as you increase the number of threads?
BONUS POINT***: Can you do something to the code so that it does not use a lock, which (seems to) eliminate(s) the race condition? If so, add that code, and explain why it (seems to) work(s)?