Fetch-and-add

In computer science, the fetch-and-add (FAA) CPU instruction atomically increments the contents of a memory location by a specified value.

The reason is that such an operation is actually implemented as multiple machine instructions: When one process is doing x = x + a and another is doing x = x + b concurrently, there is a data race.

In uniprocessor systems with no kernel preemption supported, it is sufficient to disable interrupts before accessing a critical section.

Maurice Herlihy (1991) proved that fetch-and-add has a finite consensus number, in contrast to the compare-and-swap operation.

This code only serves to help explain the behaviour of fetch-and-add; atomicity requires explicit hardware support and hence can not be implemented as a simple high level function.