0
0
Node.jsframework~10 mins

SharedArrayBuffer for shared memory in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SharedArrayBuffer for shared memory
Create SharedArrayBuffer
Create TypedArray view on buffer
Pass buffer to worker threads
Workers read/write shared memory
Use Atomics for sync
Main thread and workers see updated data
This flow shows how a shared memory buffer is created, accessed by multiple threads, and synchronized using Atomics.
Execution Sample
Node.js
const sab = new SharedArrayBuffer(4);
const arr = new Int32Array(sab);
arr[0] = 42;
// Worker reads arr[0] and updates it
// Main thread sees updated value
Creates a shared buffer, writes a value, and allows worker threads to read and update it.
Execution Table
StepActionBuffer Content (arr[0])Notes
1Create SharedArrayBuffer of 4 bytesundefinedBuffer allocated, no data yet
2Create Int32Array view on bufferundefinedTypedArray view created, no data set
3Set arr[0] = 4242Main thread writes initial value
4Worker reads arr[0]42Worker sees initial value
5Worker updates arr[0] = 100 using Atomics.store100Worker writes new value atomically
6Main thread reads arr[0] using Atomics.load100Main thread sees updated value
💡 Execution stops after main thread reads updated shared value
Variable Tracker
VariableStartAfter Step 3After Step 5Final
sab (SharedArrayBuffer)Allocated (4 bytes)Allocated (4 bytes)Allocated (4 bytes)Allocated (4 bytes)
arr[0]undefined42100100
Key Moments - 3 Insights
Why do we need Atomics to update shared memory?
Without Atomics, simultaneous writes can cause race conditions. Atomics ensure updates are done safely and visible to all threads, as shown in steps 5 and 6.
Can we use normal arrays instead of SharedArrayBuffer for sharing data?
No, normal arrays are not shared between threads. SharedArrayBuffer creates memory accessible by multiple threads, as in step 1.
Why do we create a TypedArray view on the SharedArrayBuffer?
SharedArrayBuffer is raw memory. TypedArray like Int32Array interprets this memory as numbers, allowing us to read/write values, shown in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr[0] after step 3?
Aundefined
B100
C42
D0
💡 Hint
Check the 'Buffer Content (arr[0])' column at step 3 in the execution table.
At which step does the worker update the shared memory?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look for the step where 'Worker updates arr[0]' in the Action column.
If we skip using Atomics in step 5, what might happen?
AThe update is still safe and visible immediately
BRace conditions may occur causing inconsistent data
CThe buffer size will increase automatically
DThe main thread will crash
💡 Hint
Refer to the key moment about why Atomics are needed for safe updates.
Concept Snapshot
SharedArrayBuffer creates memory shared across threads.
Use TypedArray views (e.g., Int32Array) to read/write data.
Pass the buffer to workers to share data.
Use Atomics methods to safely read/write and synchronize.
Without Atomics, data races can cause errors.
Main and workers see the same memory updates.
Full Transcript
SharedArrayBuffer allows multiple threads in Node.js to share memory safely. First, a SharedArrayBuffer is created with a fixed size in bytes. Then, a TypedArray view like Int32Array is created on top of this buffer to read and write numbers. This buffer is passed to worker threads, which can read and update the shared memory. To avoid conflicts and ensure visibility, Atomics methods are used for reading and writing. This way, all threads see consistent data. The example code shows creating a buffer, writing a value, a worker updating it atomically, and the main thread reading the updated value. This process enables efficient shared memory communication between threads.