Setup

No AI for this kata. Work it out by hand first, then run it.

package main

import (
	"fmt"
	"sync"
)

func main() {
	counter := 0
	var wg sync.WaitGroup

	for w := 0; w < 4; w++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for i := 0; i < 100_000; i++ {
				counter++
			}
		}()
	}

	wg.Wait()
	fmt.Println(counter)
}

The program runs on a machine with 4 or more CPU cores.

After you commit, do the kata itself:

  1. Explain on paper exactly how a single increment gets lost. Write out the interleaving step by step.
  2. Fix the program three ways: with a mutex, with an atomic counter, and by giving each worker its own count and adding them up at the end.
  3. Before timing them, predict which fix is fastest and which is slowest. Then measure.

Commit before you see the outcome

Your call

Before you run anything: what will this program most likely print?

Your answer

Move the slider (or use the arrow keys) to set it. 0% is a pure guess; 100% means you'd bet on it.

After you reveal, your answer and confidence are locked. That's the point.