Search⌘ K
AI Features

Fetch-And-Add

Explore the fetch-and-add instruction, a hardware primitive that atomically increments a value and returns the old one. Understand how it can be used to implement ticket locks to ensure fair thread scheduling and progress in concurrent programming. This lesson emphasizes writing concise, efficient synchronization code with real-world locking algorithms.

We'll cover the following...

One final hardware primitive is the fetch-and-add instruction, which atomically increments a value while returning the old value at a particular address. The C pseudocode for the fetch-and-add instruction looks like this:

C
int FetchAndAdd(int *ptr) {
int old = *ptr;
*ptr = old + 1;
return old;
}

TIP: LESS CODE IS BETTER CODE (LAUER’S ...