Fetch-And-Add
Let's look at one last hardware primitive for building locks, the fetch-and-add instruction.
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:
Press + to interact
int FetchAndAdd(int *ptr) {int old = *ptr;*ptr = old + 1;return old;}
TIP: LESS CODE IS BETTER CODE (LAUER’S LAW)
Programmers tend to brag about how much code they wrote to do something. Doing so is fundamentally broken. What one should brag about, rather, is how little code one wrote to accomplish a given task. Short, concise code is always preferred; it is likely easier to understand and has ...