Loading topic
ArrayBuffer, wrap it in a typed array such as Uint8Array or Int32Array, and send messages to a Web Worker or a Node.js worker thread with postMessage. The previous chapter explained that each agent (a main thread or a worker) has its own heap an5 min readSharedArrayBuffer to a worker shares memory instead of copying it. If you run that code from an ordinary web page, it often fails before it starts: SharedArrayBuffer is undefined, or the worker never gets the buffer. Browsers only give a page shared m5 min readSharedArrayBuffer is a block of raw bytes. It has no indexing operator and no notion of integers or floats, so you cannot read or write it directly. You always reach its contents through a view. A view is either a typed array such as Int32Array or a DataView. This chapter covers how views ma6 min readWorker with node:worker_threads, you know how typed arrays sit on top of an ArrayBuffer, and you can write Promise-based code. It covers how Node.js passes a SharedArrayBuffer between threads, how that differs from browsers, and the mistakes that commonl5 min readSharedArrayBuffer, wrap it in an Int32Array, and hand it to a Node.js worker thread or a Web Worker. Those steps were covered in earl6 min readSharedArrayBuffer, wrap it in a typed array view, and pass it to a worker. It also assumes you have seen why counter[0]++ loses updates when several threads run it at once. The goal here is the working model of the basic Atomics functions: what eac5 min readAtomics.add, Atomics.sub, Atomics.and, Atomics.or, and Atomics.xor cover the updates the language designers expected you to need. Real programs need others too: "store the larger of two values", "increment, but never past 100", or "claim this slot only if nobody else has". No built-in oper5 min readAtomics.load in a loop, which burns CPU, or it can wait for a postMessage, which brings back the cost of messaging. Atomics.wait and Atomics.notify add a third option. A thread sleep5 min readAtomics.wait and Atomics.notify work from the previous chapter. You should also be comfortable with Promises and async/await, and know how to pass a SharedArrayBuffer to a worker.5 min readAtomics.compareExchange and on Atomics.wait and Atomics.notify. It also assumes you can run a Node.js script that uses worker_threads. The goal is to combine those two building blocks into a mutex, a lock that lets only one thread at5 min readSharedArrayBuffer, typed array views, data races, and the basic Atomics operations, including Atomics.wait and Atomics.notify. The examples run in Node.js 16 or later with worker_threads, so you can run them without setting up cros5 min readAtomics.load, Atomics.store, Atomics.wait, Atomics.notify, and the happens-before guarantee. You should also know how to start a Node.js worker thread and pass it a SharedArrayBuffer.5 min readAtomics operations to make shared integers safe to update. Every one of those operations is atomic, but the engine can make an operation atomic in two different ways. It can use a single hardware instruction, or it can guard the memory with a hidden lock. Atomics.isLockFree5 min readSharedArrayBuffer makes you pick its size when you create it. That is a problem when you can't know the size in advance, such as a shared log, a work queue that fills up, or a decoded file whose size you only learn while you parse it. The old fixes were to allocate far more than you n6 min readSharedArrayBuffer, how to read and write it with Atomics, how to block a thread with Atomics.wait, and how the mutex and ring buffer patterns work. It also assumes you know how postMessage uses5 min read