Simple, Simon and Sage signaling.
Gemini 2.5 September 2025
Thread synchronization puzzle
by: burt rosenberg & ChatGPT
at: university of miami and cyberspace
date: oct 2025
Goals
- Learn about concurrency.
Problem Description
The book The Little Book on Semaphores
by Allen Downey poses this problem (page 6)
Suppose that 100 threads run the following program concurrently
(if you are not familiar with Python, the for loop runs the update 100 times):
1 for i in range (100):
2 temp = count
3 count = temp + 1
What is the largest possible value of count after all threads have completed?
What is the smallest possible value?
Hint: the first question is easy; the second is not
I have programmed up this problem,
where I have only 5 threads and the count is 5.
I have produced solutions for a final value of count of 25, 10 and 5. I have done
this only by manipulating concurrency by only using sleep statements, to
make possible (just improbable) times probable.
Each thread has an index, 0 through 4, and in some cases the sleep statements are
conditioned on the thread index. For instance, I can hold off the zero thread for
starting until all others are done with,
if (!data->index) sleep(6) ;
Your task is to
- Modify the code in the same way (only sleep, perhaps conditioned on thread index)
to get two other final values.
- To answer the question: what is the smallest possible value?
- And modify the code using only sleeps, possibly conditioned on thread index,
to achieve this final value.
Please note neither the values in task (1) should be equal to that in task (3).
Submit as three programs,
- threads_counter_a.c (task 1)
- threads_counter_b.c (task 1)
- threads_counter_c.c (task 2 & 3)