Please disable your adblock and script blockers to view this page

Using WebAssembly threads from C, C++ and Rust


WebAssembly
JavaScript
WebAssembly.
Web Workers
the JavaScript API
ArrayBuffer
Spectre
Site Isolation
Chrome
Firefox
COOP
COEP
POSIX Threads
Emscripten
GCC
UI
PTHREAD_POOL_SIZE_STRICT=0.Second
Rust
Squoosh.app
AVIF
WebAssembly SIMD!Google Earth
FFmpeg
the Creative Commons Attribution


Clang
PROXY_TO_PTHREAD
Wasm
Rayon
threads.wasm-bindgen-rayon
WebP

No matching tags


JavaScript


the Google Developers Site Policies


JavaScript
Emscripten
Comlink

No matching tags

Positivity     37.00%   
   Negativity   63.00%
The New York Times
SOURCE: https://web.dev/webassembly-threads/
Write a review: Hacker News
Summary

It allows you to either run parts of your code in parallel on separate cores, or the same code over independent parts of the input data, scaling it to as many cores as the user has and significantly reducing the overall execution time.In this article you will learn how to use WebAssembly threads to bring multithreaded applications written in languages like C, C++, and Rust to the web.WebAssembly threads is not a separate feature, but a combination of several components that allows WebAssembly apps to use traditional multithreading paradigms on the web.First component is the regular Workers you know and love from JavaScript. Emscripten provides an API-compatible implementation of the pthread library built atop Web Workers, shared memory and atomics, so that the same code can work on the web without changes.Let's take a look at an example:example.c:Here the headers for the pthread library are included via pthread.h. You can also see a couple of crucial functions for dealing with threads.pthread_create will create a background thread. Check out the blog post about Using asynchronous web APIs from WebAssembly if you'd like to learn more.In this case, the code synchronously invokes pthread_create to create a background thread, and follows up by another synchronous call to pthread_join that waits for the background thread to finish execution. Incidentally, when using this option, you don't have to precreate the thread pool either—instead, Emscripten can leverage the main thread for creating new underlying Workers, and then block the helper thread in pthread_join without deadlocking.Third, if you're working on a library and still need to block, you can create your own Worker, import the Emscripten-generated code and expose it with Comlink to the main thread. The only new thing you gain is access to higher-level APIs like std::thread and std::async, which use the previously discussed pthread library under the hood.So the example above can be rewritten in more idiomatic C++ like this:example.cpp:When compiled and executed with similar parameters, it will behave in the same way as the C example:Output:Unlike Emscripten, Rust doesn't have a specialized end-to-end web target, but instead provides a generic wasm32-unknown-unknown target for generic WebAssembly output.If Wasm is intended to be used in a web environment, any interaction with JavaScript APIs is left to external libraries and tooling like wasm-bindgen and wasm-pack. Instead, you should create a Worker, import the wasm-bindgen-generated code there, and expose its API with a library like Comlink to the main thread.Check out the wasm-bindgen-rayon example for an end-to-end demo showing:We actively use WebAssembly threads in Squoosh.app for client-side image compression—in particular, for formats like AVIF (C++), JPEG-XL (C++), OxiPNG (Rust) and WebP v2 (C++).

As said here by