Skip to content

Pthreads with WebAssembly

Derek Schuff edited this page Dec 1, 2017 · 18 revisions

Emscripten has support for compiling with pthreads, using asm.js, Web Workers, and SharedArrayBuffer. That support is documented here. WebAssembly has a proposed spec for threads (or more properly, for a shared linear memory and atomic memory instructions), with an overview here and spec here. This spec has experimental implementations in Firefox Nightly and Chrome Canary.

Emscripten now also has experimental support for the wasm threading spec. To build a simple threaded program: emcc program.c -s USE_PTHREADS=1 -s WASM=1 -o program.html (it may also be desirable to use -s PTHREAD_POOL_SIZE as documented in the emscripten threading doc). To test with Chrome Canary, use the following flags: --js-flags=--experimental-wasm-threads --enable-features=WebAssembly --disable-features=WebAssemblyTrapHandler. Firefox nightly has all experimental features turned on by default, so it should just work.