linux-sysprog · intermediate · ~15 min

Detect a lost update

Recognise the symptom of an unsynchronised increment race.

Challenge

Two threads each increment a shared counter. Implement int lost_update(int initial, int incs_a, int incs_b, int observed) returning 1 if observed is less than the correct total (initial + incs_a + incs_b), indicating a lost update, else 0.

Starter code

int lost_update(int initial, int incs_a, int incs_b, int observed) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.