WebAssembly Threads: SharedArrayBuffer and Concurrency in the Browser

Parallel browser threads. We explore Wasm threading models, Web Workers, and mutex operations in Wasm.

VP
SHIVAM ITCS
·23 January 2019·10 min read·1 views

Technical Overview & Strategic Context

While WebAssembly MVP enabled near-native execution speeds for single-threaded tasks, it lacked support for parallel thread execution, limiting its performance for heavy workloads (like video editing or physics engines) that rely on multi-core scaling. In early 2019, browser vendors addressed this by introducing WebAssembly Threads support, allowing Wasm modules to run concurrently across multiple browser threads using Web Workers.

Architectural Principle: Use WebAssembly Threads to accelerate computation-heavy browser tasks. Manage shared memory buffers (SharedArrayBuffer) carefully to prevent data races.

Core Concepts & Architectural Blueprint

WebAssembly Threads utilizes Web Workers to run parallel execution threads. Threads share memory allocations using a SharedArrayBuffer, allowing them to read and write data in the same memory space without serialization overhead. Wasm uses atomic instructions (like wake and wait) to synchronize execution, preventing thread conflicts.

Performance & Capability Comparison

Execution ModelSingle-Thread Wasm MVPMulti-Thread WasmPerformance Impact
CPU UtilizationLimited to a single CPU coreScales across available CPU coresAccelerates numerical calculations
Data SharingRequires postMessage serializationDirect memory access (SharedArrayBuffer)Eliminates data transfer overhead
SynchronizationNo synchronization primitives neededAtomic instructions (locks, mutexes)Requires careful concurrency management

Implementation & Code Pattern

To load and execute a multi-threaded WebAssembly module in a web application, follow these steps:

  • Compile systems source code into WebAssembly with thread support enabled.
  • Initialize Web Workers to act as background execution threads.
  • Pass SharedArrayBuffer instances to the Wasm module to allocate memory.
  • Use atomic operations to synchronize thread access to shared memory.
javascriptcode
// Loading WebAssembly with thread support using Web Workers (2019)
// main.js
const memory = new WebAssembly.Memory({
  initial: 10,
  maximum: 100,
  shared: true // Allocate SharedArrayBuffer memory
});

const worker = new Worker('worker.js');
// Share memory and Wasm module with the worker thread
worker.postMessage({ module: compiledModule, sharedMemory: memory });

// worker.js
self.onmessage = async (event) => {
  const { module, sharedMemory } = event.data;
  const instance = await WebAssembly.instantiate(module, {
    env: { memory: sharedMemory }
  });
  
  // Execute thread-specific computations in Wasm
  instance.exports.perform_calculations();
};

Operational Governance & Future Outlook

WebAssembly Threads enabled developers to run parallel, multi-core computations inside the browser sandbox, expanding the capabilities of web-based systems.

VP
Vijay Paliwal
Founder, SHIVAM ITCS · 18+ years enterprise & AI engineering
MCA · Ex-HiveGPT USA · Ex-Social27 Seattle
WebAssembly Threads: SharedArrayBuffer and Concurrency in the Browser | SHIVAM ITCS Blog | SHIVAM ITCS