int compare_and_swap(int *ptr, int expected, int new) { int actual = *ptr; if (actual == expected) *ptr = new; return actual; }