You're given a multi-threaded C program with three threads, each incrementing a shared global variable count 1,000,000 times (M = 1000000). The total number of increments attempted is 3 * 1,000,000 = 3,000,000.
The variable count is declared volatile, but there is no synchronization (e.g., mutexes, atomic operations). Therefore, race conditions occur when multiple threads read, increment, and write back count concurrently.
✅ Maximum possible value: 3,000,000
This happens when no race condition interferes — i.e., each increment is atomic and completed without interference. This is theoretically possible but extremely unlikely without synchronization.
❓ Minimum possible value due to race condition?
We need to find the minimum possible final value of count that can result from worst-case interleaving of thread operations.
🔍 Understanding the Race Condition
The operation:
count = count + 1;
is not atomic. It involves three steps:
- Read the current value of
count into a register.
- Increment the register value.
- Write the new value back to
count.
If two threads do this simultaneously, they might:
- Both read the same value of
count (say, 50).
- Both increment it to 51.
- Both write 51 back.
→ Result: One increment is lost.
Each such collision causes one lost increment.
🧠 Goal: Maximize the number of lost increments → Minimize final count
We want to minimize the final value of count. To do that, we need to maximize the number of overlapping read-modify-write sequences that cause lost updates.
Each thread does 1,000,000 increments → 3,000,000 total operations.
Each increment operation is a sequence of 3 steps: R (read), I (increment), W (write).
Let’s denote each increment as a triple: (R, I, W)
We have 3,000,000 such triples (1M per thread × 3 threads).
The minimum possible value of count occurs when as many increments as possible are lost due to overlapping reads.
💡 Key Insight: Worst-case scenario
In the worst-case interleaving, every increment operation is overwritten by another thread immediately after reading the same value.
Suppose all three threads are perfectly synchronized to read the same value at the same time, then increment and write.
Let’s think of it as a sequence of atomic steps.
Suppose count starts at 0.
Consider the first increment by each thread:
- Thread 0: reads
count = 0
- Thread 1: reads
count = 0
- Thread 2: reads
count = 0
Then:
- Thread 0: writes
1
- Thread 1: writes
1 → overwrites 1 → still 1
- Thread 2: writes
1 → overwrites again → still 1
→ Result: Three increments attempted, but only one is reflected → 2 lost.
Similarly, for the next round:
- All three read
1
- All three compute
2
- Only the last write wins →
count = 2
→ Again, 2 lost increments
So, for every group of 3 concurrent increments, we lose 2.
This can happen repeatedly if the threads are perfectly interleaved.
Thus, in the worst case, every increment except one per "batch" is lost.
Since there are 3,000,000 increment operations, and every 3 operations can result in only 1 effective increment, the minimum possible value is:
3,000,000 / 3 = 1,000,000
✅ Minimum possible value of count = 1,000,000
🚀 Execution Chain Producing Minimum Value
We need an execution sequence where each time a thread reads count, the other two threads also read the same value before any of them writes back, and the last writer overwrites the other two.
Here’s how it can happen:
We assume the scheduler interleaves the three threads perfectly in a round-robin fashion, and each thread reads the value just before another thread writes.
Example with small M = 1 per thread (total 3 increments):
We want final count = 1, not 3.
Execution trace:
Thread 0: read count → 0
Thread 1: read count → 0
Thread 2: read count → 0
Thread 0: write 1 → count = 1
Thread 1: write 1 → count = 1 (overwrites)
Thread 2: write 1 → count = 1 (overwrites)
Result: count = 1 → 2 lost
Now repeat this pattern for all 1,000,000 rounds.
So for each value v from 0 to 999,999:
- All three threads read
v
- All three compute
v+1
- Only the last write (say, Thread 2) survives →
count = v+1
After 1,000,000 such rounds:
→ count = 1,000,000
✅ Therefore, the minimum value is 1,000,000
🔁 Why not lower than 1,000,000?
Because each thread must perform 1,000,000 increments. Even if all writes are overwritten, each thread still has to complete its loop.
The only way to have fewer than 1,000,000 is if some thread never gets to write, or if some reads/writes are lost entirely — but the problem says each thread runs for (int j=0; j<M; j++) count = count + 1; — so all 3M operations execute.
But due to race conditions, many writes are overwritten.
The minimum number of distinct values count can reach is 1,000,000, because:
- The first thread to write after all read 0 → sets it to 1
- Then the next group sets it to 2
- ...
- After 1,000,000 such groups, it reaches 1,000,000
You cannot skip values — you can’t go from 0 to 3 without passing 1 and 2.
And you cannot have fewer than 1,000,000 because each thread must complete its 1M iterations, and the only way to lose increments is to overwrite — but you still need to reach each integer from 0 to 1,000,000 at least once.
Actually, more precisely: you can have at least one thread’s increment survive per "round", so you get at least 1,000,000.
You cannot get below 1,000,000 because even in the worst case, each thread's 1M increments are performed, and the last write in each batch still increments the counter by 1. Since there are 1M batches (each batch = 3 increments), you get 1M final value.
✅ Final Answer:
Minimum possible value of count: 1,000,000
Execution chain producing this:
The three threads are perfectly interleaved such that in each group of three consecutive read-modify-write sequences (one from each thread), all three threads read the same current value of count, compute value + 1, and only the last thread to write succeeds. This happens 1,000,000 times, resulting in count being incremented only once per group, for a total of 1,000,000.
This is the worst-case race condition scenario.
💡 Note:
This assumes a cooperative or perfectly scheduled interleaving — which is possible on single-core systems with preemption between instructions, or on multi-core systems with cache coherency delays. In practice, such perfect interleaving is rare, but theoretically possible, so it’s the minimum bound.
✅ Final Boxed Answer:
Minimum value of count:
1000000
Execution chain:
All three threads repeatedly read the same value of count before any writes, compute value + 1, and only the last write in each group of three survives, repeating this 1,000,000 times.