| Crates.io | wasm-bindgen-spawn |
| lib.rs | wasm-bindgen-spawn |
| version | 0.0.5 |
| created_at | 2024-10-08 06:23:48.628715+00 |
| updated_at | 2026-01-10 19:04:54.749447+00 |
| description | Web Worker Multithreading library for wasm-bindgen the uses shared memory |
| homepage | |
| repository | https://github.com/Pistonite/wasm-bindgen-spawn |
| max_upload_size | |
| id | 1400839 |
| size | 30,385 |
A Web Worker based multithreading library for Rust and WebAssembly.
This uses the WebAssembly threads proposal
and shared memory to communicate between workers (once they are started), instead of postMessage.
The threads proposal is currently in phase 4 and available in Chrome, Firefox, Safari and Node.js
At the current stage, this is the closest thing to std::thread::spawn
that "Just Works" for wasm32-unknown-unknown target. You can:
std::sync primitivesNightly Rust toolchain is required for unstable features. This library
will remain on version 0.0.x until all features required are in stable Rust,
standardized in WASM, and baseline widely available across browsers.
The examples directory
on GitHub contains a full example using Vite. Check out the live demo
See ThreadCreator for the main API.
I wrote a blog on how and why this library is designed this way, and what the limitations are. You can read it here.
You can read more about this in the web dev article. Long story short:
SharedArrayBufferTo get started, the server that serves the main document must send these headers:
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
You can check if the document is in a cross-origin isolated context by running this in the console:
self.crossOriginIsolated
Read the full article for more details on the implications of Cross-Origin Isolation.
target-featurerust-toolchain file (no extensions) and put nightly in it, to use the nightly toolchain
echo "nightly" > rust-toolchain
.cargo/config.toml
[target.wasm32-unknown-unknown]
rustflags = [
"-C", "target-feature=+atomics",
"-Clink-args=--shared-memory",
"-Clink-args=--import-memory",
"-Clink-args=--max-memory=1073741824",
"-Clink-args=--export=__wasm_init_tls",
"-Clink-args=--export=__tls_size",
"-Clink-args=--export=__tls_align",
"-Clink-args=--export=__tls_base",
]
# You also need `bulk-memory` for Rust < 1.87. For 1.87+ it's enabled by default
# rustflags = ["-C", "target-feature=+atomics,+bulk-memory"]
[unstable]
build-std = ["panic_abort", "std"]
wasm-pack TargetCurrently, this library only supports the no-modules target:
wasm-pack build -t no-modules
Since the main thread in web cannot block, you must use blocking operations in a web worker, this include:
join on a JoinHandlerecv on a Receiver in the std::sync library or oneshot library.The example shows how to put the WASM module in the worker. You can
then use some kind of RPC with postMessage to communicate between the main thread and the worker.
This is probably something you have to do anyway to avoid the heavy, multithreaded computation freezing the UI.