Sub topic 14
Checking Lock-Free Sizes with Atomics.isLockFree
This chapter assumes you already know the basic Atomics operations (load, store, add, compareExchange) and the typed array views from earlier chapters: Int8Array, Int16Array, Int32Array, and BigInt64Array.
What "lock-free" means here
Each Atomics call is indivisible. No other thread can see it half done. An engine can provide that guarantee in two ways:
- Hardware atomics. The CPU has a single instruction that reads, modifies, and writes a value of that size in one step. The operation is lock-free.
- An internal lock. The engine takes a hidden lock, performs the operation, and releases the lock. The result is still correct. It is slower, and threads queue up behind one another.
Atomics.isLockFree(size) tells you which of these the engine uses for elements that are size bytes wide. It returns true for hardware atomics and false for the lock.
Running the check
The script below works the same in Node.js and in a browser console.
const sizes = [1, 2, 3, 4, 8, 16];
for (const size of sizes) {
console.log(`${size} bytes: ${Atomics.isLockFree(size)}`);
}On a typical x86-64 or ARM64 machine running V8, the output is:
1 bytes: true
2 bytes: true
3 bytes: false
4 bytes: true
8 bytes: true
16 bytes: falseThe specification guarantees only one of these results.
| Size (bytes) | Matching typed arrays | Guarantee |
|---|---|---|
| 1 | `Int8Array`, `Uint8Array` | Depends on the implementation |
| 2 | `Int16Array`, `Uint16Array` | Depends on the implementation |
| 4 | `Int32Array`, `Uint32Array` | Always `true` |
| 8 | `BigInt64Array`, `BigUint64Array` | Depends on the implementation |
| Other | None | `false` |
Sizes 3 and 16 return false because no typed array has those element sizes. The question has no meaning for them.
Using the result
In most code you don't need to call isLockFree at all. Pick Int32Array for counters, flags, and lock words. Its size is the one the specification guarantees to be lock-free, and Atomics.wait and Atomics.notify work on it.
The check is useful when you want a wider or narrower type for a heavily contended value and want to know what it costs. For example, a 64-bit counter needs BigInt64Array:
const sab = new SharedArrayBuffer(8);
if (!Atomics.isLockFree(8)) {
console.warn('64-bit atomics use a lock here; contended updates may be slow');
}
const counter = new BigInt64Array(sab);
Atomics.add(counter, 0, 1n);
console.log(Atomics.load(counter, 0)); // 1nThe code is correct on every engine. The warning only tells you to expect slower updates under contention.
Common mistakes
- Treating
falseas unsafe. Some developers fall back to plain reads and writes when the check fails. That fallback creates exactly the data race Atomics exists to prevent. - Passing a byte length or an element count. The argument is the element size in bytes, such as
Int16Array.BYTES_PER_ELEMENT. It is notsab.byteLength. - Caching the answer across machines. The result depends on the CPU and the engine, so check at runtime if it matters to you.
Exercise
Atomics.isLockFree(2) returns false on some platform. A teammate proposes replacing Atomics.add on an Int16Array with view[i] += 1 "since atomics aren't supported for 16-bit values anyway." What is wrong with this, and what would you suggest?
Show answer
The false result only means the engine uses an internal lock for 16-bit atomics. Atomics.add on an Int16Array is still atomic and correct. Replacing it with view[i] += 1 creates a data race that loses updates. Keep Atomics.add. If speed under contention matters, switch the value to an Int32Array, because 4-byte atomics are guaranteed to be lock-free.