Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Race conditions and interleavings card 3 of 6

Two shapes, two kinds of race

Look for shared state in two shapes: - **Read-modify-write**: read, compute, write back (`+=`, append, update a field). - **Check-then-act**: decide on a check, then act. The fact can change in between. **Data race**: two threads access the same memory, at least one writes, and nothing synchronizes them. **Logical race**: each access is synchronized, but the multi-step sequence is not. Only synchronization creates ordering: locks, atomics, channels and tools built on them, such as Go's `sync.WaitGroup`. A sleep just makes one order likely.
Terms on this card
Read-modify-write
Reading a shared value, computing a new one from it and writing it back. It is unsafe if another thread can act between the read and the write.
Check-then-act
Testing a condition and then acting on it, for example 'if missing, create'. The condition can become false between the check and the act.
Data race
Two threads access the same memory, at least one of them writes, and no synchronization orders the two accesses.
Logical race
A race where every single access is synchronized, but a multi-step sequence is not, so a bad interleaving still breaks the program's logic.
sync.WaitGroup
A Go type that lets one goroutine wait until a set of other goroutines have called Done, which orders their writes before the wait returns.
Learn this step by step on Bytewise
Lesson goal: Explain how concurrent code can interleave and produce wrong results, using check-then-act and read-modify-write as examples. Distinguish a data race from a logical race, and explain why only synchronization (locks, atomics, channels) creates ordering while timing does not. · the whole lesson