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 6 of 6
Order comes only from synchronization
Concurrent steps can interleave in any order, and a green run shows only one of those orders. Find read-modify-write and check-then-act on shared state. Fix them with a lock, an atomic or a channel that covers the **whole** sequence, never with a sleep. Locking every single access prevents data races, but not logical races.
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.
- atomic
- Done as one indivisible step that other threads cannot observe half-done or interrupt. Atomic types (for example atomic integers) provide this for single operations.
- channel
- In Go, a typed pipe between goroutines. A send is guaranteed to happen before the matching receive completes, which creates ordering.
- green
- A test or check that passes.
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