Race conditions & synchronisation
P32.threads-processes.03 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Executors, futures & queues
P32.threads-processes.03 · Audience: Guest, IT/ML learner, Language professional · Prerequisites: Executors, futures & queues
The moment two threads touch the same variable, correctness is on the line. This
module reproduces a real data race, fixes it with a Lock, and then bounds
concurrency with a Semaphore — the two synchronisation primitives you reach for
most. All on the kernel, so the races are real.
balance = balance + amount reads the balance, computes the sum, then writes it back. If another thread reads the same starting balance in that gap, both write over each other and one update is lost. That gap is the data race.Why it matters — Data races are the hardest bugs to reproduce and the easiest to ship — understanding the read-modify-write gap is how you spot them in review.
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Fix a losing bank account with a Lock, then cap concurrency with a
Semaphore — both on the kernel, where the races actually happen.
Loading exercise…
Loading exercise…
Threads and processes give you parallelism; locks and semaphores keep it correct. The pillar's last track steps back to the question you should ask before writing any of this: which concurrency model does the job even need?
Loading your notes...