@cmpxchgStrong
@cmpxchgStrong
@cmpxchgStrong(comptime T: type, ptr: *T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) ?T
This function performs a strong atomic compare-and-exchange operation, returning null if the current value is not the given expected value. It’s the equivalent of this code, except atomic:
not_atomic_cmpxchgStrong.zig
fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_value: T) ?T {
const old_value = ptr.*;
if (old_value == expected_value) {
ptr.* = new_value;
return null;
} else {
return old_value;
}
}
If you are using cmpxchg in a retry loop, @cmpxchgWeak is the better choice, because it can be implemented more efficiently in machine instructions.
T must be a pointer, a bool, a float, an integer or an enum.
@typeInfo(@TypeOf(ptr)).Pointer.alignment must be >= @sizeOf(T).
AtomicOrder can be found with @import("std").builtin.AtomicOrder.
See also:
- @atomicStore
- @atomicLoad
- @atomicRmw
- @fence
- @cmpxchgWeak
