4. Other Hardware Primitives
-
Compare-and-swap:
int CompareAndSwap(int *ptr, int expected, int new);
returns the old value and simultaneously updates to the new value if old == expected.
Fetch-and-add:int FetchAndAdd(int *ptr);
returns the old value and simultaneously adds 1 and stores the new value.Can be used to implement ticket lock and ensure fairness.