Tasks /
Concurrency /
Thread-safe counter
Statement
medium
Thread-safe counter
`Counter` stores a number. `increment()` adds one and deliberately pauses inside
the work so the race is reproducible.
Write tests:
- one thread: two increments give 2;
- six threads with five increments each give 30.
Start the threads through `threading.Barrier` so they begin at the same time.
Do not check intermediate values: only the final total.
AI mentor
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and ask
Hints
1 of 3 unlocked · attempts: 0
Hint 1
What happens if two threads read the same value before writing? How many
increments are lost?
Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts