JS Built-ins
AtomicscompareExchangebeginner

Atomics.compareExchange returns old value

πŸ“– Usage Example
const i32 = new Int32Array(new SharedArrayBuffer(4))
Atomics.store(i32, 0, 5)
Atomics.compareExchange(i32, 0, 5, 10)  // β†’ 5 (old val)
Atomics.load(i32, 0)  // β†’ 10

compareExchange() updates a value only if it currently equals the expected value.

Use Atomics.compareExchange to get the old value at an index before exchanging.

πŸ’‘ Hints (1)
  • β€’ Use Atomics.compareExchange(typedArray, index, expected, replacement)