Sub topic 4
Reading and Writing Shared Memory Through Typed Arrays
This chapter assumes you already know how typed arrays such as Int32Array work over an ordinary ArrayBuffer, and that you have created a SharedArrayBuffer as shown in the previous chapter.
A SharedArrayBuffer is raw bytes with no methods for reading or writing values. To use it, you put a typed array view over it. A view tells JavaScript how to read the bytes: as 32-bit integers, as single bytes, and so on. Every agent creates its own view object, but all of those views point at the same memory.
Several Views over One Buffer
You can place more than one view over a single buffer. A write through one view shows up immediately in every other view that covers those bytes.
const sab = new SharedArrayBuffer(16);
const ints = new Int32Array(sab); // 4 elements over all 16 bytes
const bytes = new Uint8Array(sab); // 16 elements over the same bytes
const tail = new Int32Array(sab, 8, 2); // starts at byte 8, holds 2 elements
ints[0] = 0x01020304;
console.log(Array.from(bytes.subarray(0, 4))); // [4, 3, 2, 1] on little-endian hardware
tail[0] = 99;
console.log(ints[2]); // 99: tail[0] and ints[2] are the same bytes
console.log(tail.byteOffset, tail.length); // 8 2When you postMessage a typed array whose buffer is shared, the receiver gets a new view object, but the buffer underneath is still the same memory. Sending only the buffer and building views on each side is just as valid, and it makes the layout explicit.
Common Errors
Misaligned Offsets
A view's byteOffset must be a multiple of its element size. This line throws a RangeError:
const sab = new SharedArrayBuffer(16);
new Int32Array(sab, 2); // RangeError: offset must be a multiple of 4If you pack several fields into one buffer, place the larger types first, or pad the offsets so each field starts on a multiple of its own size.
Assuming Byte Order
Typed arrays use the platform's native byte order, which is little-endian on almost all current hardware. That is fine when every thread reads through the same view type. If you mix element sizes, or exchange bytes with a file or the network, use a DataView and pass the byte order explicitly:
const view = new DataView(new SharedArrayBuffer(4));
view.setInt32(0, 0x01020304, false); // big-endian
console.log(view.getUint8(0)); // 1Choosing a Type That Atomics Rejects
| View type | Works with `Atomics` operations | Works with `Atomics.wait` |
|---|---|---|
| `Int8Array`, `Uint8Array`, `Int16Array`, `Uint16Array`, `Uint32Array` | Yes | No |
| `Int32Array`, `BigInt64Array` | Yes | Yes |
| `BigUint64Array` | Yes | No |
| `Float32Array`, `Float64Array`, `Uint8ClampedArray` | No | No |
| `DataView` | No | No |
Int32Array is the safe default for shared counters and flags, because every Atomics function accepts it.
Plain Indexing Is Not Coordination
ints[0] = 5 writes to shared memory, but nothing guarantees when another thread will see the write, or whether a read-then-write sequence will be interrupted. The next chapters explain why and how to fix it.
Exercise
You create const sab = new SharedArrayBuffer(12) and want an Int32Array view covering only the last 8 bytes. Write the constructor call, then say what new Float64Array(sab, 4, 1) does and why.
Show answer
new Int32Array(sab, 4, 2) starts at byte 4 and holds two 4-byte elements. new Float64Array(sab, 4, 1) throws a RangeError, because a Float64Array's offset must be a multiple of 8 and 4 is not.
Exercise
A worker needs to sleep with Atomics.wait until a shared status value changes. Which view types can hold that value?
Show answer
Int32Array or BigInt64Array. Atomics.wait accepts only these two types.