Enabling WebAssembly Threads with Pthreads in Chrome via Origin Trials
This article explains how to compile C/C++ code to WebAssembly with pthread support, run multithreaded WebAssembly modules in Chrome using Origin Trials, and provides step‑by‑step instructions, example code, and configuration details for developers.
WebAssembly (Wasm) can compile code written in C++ and other languages to run on the web, and it now supports threading through the standard pthreads API, enabling parallel computation similar to native applications.
The WebAssembly community group has been adding thread support, and V8 provides the necessary implementation that can be accessed via an Origin Trial, allowing developers to experiment with the feature before it becomes a standard.
Origin Trial information: https://github.com/GoogleChrome/OriginTrials
Chrome 70 and later support WebAssembly threads, and developers are encouraged to try them and provide feedback.
Threads? How about Workers?
Since Chrome 4, browsers have used Web Workers for parallelism, but workers do not share mutable data and communicate via message passing. Each worker runs in an isolated V8 isolate, so they cannot share memory like pthreads.
WebAssembly threads, however, can share the same Wasm memory using SharedArrayBuffer . Each Wasm thread runs inside a Web Worker but shares memory, allowing code that uses pthreads to run with true multithreading on the web.
A Simple Example
The example starts with a main() function that declares two variables, fg_val and bg_val , and a fibonacci() function that is called by two threads: the main (foreground) thread computes fg_val , while a background thread created with pthread_create() computes bg_val . After the background thread finishes, the results are printed.
Compiling with Thread Support
First install the Emscripten SDK (version 1.38.11 or newer). To build the example with thread support you must pass extra flags to the emcc compiler. The command line looks like this:
<!DOCTYPE html>
<html>
<title>Threads test</title>
<body>
<script src="test.js"></script>
</body>
</html>The flag -s USE_PTHREADS=1 enables thread support in the compiled Wasm module, and -s PTHREAD_POOL_SIZE=2 tells the compiler to generate a pool of two threads.
When the program runs, the WebAssembly module loads, creates a Web Worker for each thread in the pool, and each worker shares the same memory instance, allowing them to cooperate. V8 7.0 improves sharing of compiled native code between workers, enabling large applications to scale across many workers. Ensure the thread‑pool size matches the maximum number of threads your application needs; otherwise thread creation may fail or you may waste resources.
How to Try It
Enable experimental WebAssembly thread support in Chrome 70+ by navigating to chrome://flags and turning on the “WebAssembly Threads” flag, then restart the browser.
After restarting, you can load a minimal HTML page that loads the threaded WebAssembly module, for example:
<!DOCTYPE html>
<html>
<title>Threads test</title>
<body>
<script src="test.js"></script>
</body>
</html>Serve this page via a web server and open it in Chrome; the DevTools console will show output confirming that the threaded WebAssembly program executed successfully.
Testing with Origin Trial
For development and debugging you can enable the experimental flag, but for real‑world testing you should use an Origin Trial token bound to your domain. Deploy your application with the token and users with supported browsers (Chrome 70+) will be able to run the threaded Wasm code.
WebAssembly threads provide a powerful new primitive for porting C/C++ applications and libraries that rely on pthreads to the web, enabling true multithreaded execution in the browser.
UC Tech Team
We provide high-quality technical articles on client, server, algorithms, testing, data, front-end, and more, including both original and translated content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.