{"id":18016632,"url":"https://github.com/pistonite/wasm-bindgen-spawn","last_synced_at":"2025-08-17T20:32:22.579Z","repository":{"id":257814913,"uuid":"869316649","full_name":"Pistonite/wasm-bindgen-spawn","owner":"Pistonite","description":"A Web Worker based multithreading library for Rust and Assembly","archived":false,"fork":false,"pushed_at":"2024-10-14T02:08:09.000Z","size":147,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-17T19:10:09.895Z","etag":null,"topics":["atomics","rust","shared-memory","threading","wasm","wasm-bindgen"],"latest_commit_sha":null,"homepage":"https://docs.rs/wasm-bindgen-spawn","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pistonite.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-08T05:10:14.000Z","updated_at":"2024-10-14T02:08:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"be811022-2927-452a-b2ea-4f907ef261b3","html_url":"https://github.com/Pistonite/wasm-bindgen-spawn","commit_stats":null,"previous_names":["pistonite/wasm-bindgen-spawn"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pistonite%2Fwasm-bindgen-spawn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pistonite%2Fwasm-bindgen-spawn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pistonite%2Fwasm-bindgen-spawn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pistonite%2Fwasm-bindgen-spawn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pistonite","download_url":"https://codeload.github.com/Pistonite/wasm-bindgen-spawn/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230167800,"owners_count":18183845,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["atomics","rust","shared-memory","threading","wasm","wasm-bindgen"],"created_at":"2024-10-30T04:18:43.759Z","updated_at":"2025-08-17T20:32:22.534Z","avatar_url":"https://github.com/Pistonite.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# wasm-bindgen-spawn\r\n\r\n![Build Badge](https://img.shields.io/github/check-runs/Pistonite/wasm-bindgen-spawn/main)\r\n![Version Badge](https://img.shields.io/crates/v/wasm-bindgen-spawn)\r\n![License Badge](https://img.shields.io/github/license/Pistonite/wasm-bindgen-spawn)\r\n![Issue Badge](https://img.shields.io/github/issues/Pistonite/wasm-bindgen-spawn)\r\n\r\nA Web Worker based multithreading library for Rust and WebAssembly.\r\n\r\nThis uses the WebAssembly [threads proposal](https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md)\r\nand shared memory to communicate between workers (once they are started), instead of `postMessage`.\r\nThe threads proposal is currently in [phase 4](https://webassembly.org/features/) and available in Chrome, Firefox, Safari and Node.js\r\n\r\nAt the current stage, this is the closest thing to `std::thread::spawn`\r\nthat \"Just Works\" for `wasm32-unknown-unknown` target. You can:\r\n- Spawn a thread with a Rust closure\r\n- Join a thread\r\n- Send data between threads using channels\r\n- Synchronize threads using `std::sync` primitives\r\n\r\nNightly Rust toolchain is required for unstable features. This library\r\nwill remain on version `0.0.x` until all features required are in stable Rust,\r\nstandardized in WASM, and baseline widely available across browsers.\r\n\r\n## Examples\r\nThe [`examples`](https://github.com/Pistonite/wasm-bindgen-spawn/tree/main/example) directory \r\non GitHub contains a full example using Vite. Check out the [live demo](https://wasm-bindgen-spawn.pistonite.org)\r\n\r\nSee [ThreadCreator](https://docs.rs/wasm-bindgen-spawn/latest/wasm_bindgen_spawn/struct.ThreadCreator.html) for the main API.\r\n\r\n## Background/Design\r\nI wrote a blog on how and why this library is designed this way,\r\nand what the limitations are. You can read it [here](https://pistonite.github.io/wasm-bindgen-spawn).\r\n\r\n## Requirements\r\n\r\n### Cross-Origin Isolation\r\n\r\nYou can read more about this in the [web dev article](https://web.dev/articles/coop-coep). Long story short:\r\n- This is required for `SharedArrayBuffer`\r\n- This is to mitigate Spectre-like attacks\r\n\r\nTo get started, the server that serves the main document must send these headers:\r\n```\r\nCross-Origin-Embedder-Policy: require-corp\r\nCross-Origin-Opener-Policy: same-origin\r\n```\r\n\r\nYou can check if the document is in a cross-origin isolated context by running this in the console:\r\n```javascript\r\nself.crossOriginIsolated\r\n```\r\n\r\nRead the full article for more details on the implications of Cross-Origin Isolation.\r\n\r\n### Rust Nightly and `target-feature`\r\n1. Create a `rust-toolchain` file (no extensions) and put `nightly` in it, to use the nightly toolchain\r\n    ```sh\r\n    echo \"nightly\" \u003e rust-toolchain\r\n    ```\r\n2. Add the following to `.cargo/config.toml`\r\n    ```toml\r\n    [target.wasm32-unknown-unknown]\r\n    rustflags = [\"-C\", \"target-feature=+atomics\"]\r\n    # You also need `bulk-memory` for Rust \u003c 1.87. For 1.87+ it's enabled by default\r\n    # rustflags = [\"-C\", \"target-feature=+atomics,+bulk-memory\"]\r\n\r\n    [unstable]\r\n    build-std = [\"panic_abort\", \"std\"]\r\n    ```\r\n\r\n### `wasm-pack` Target\r\nCurrently, this library only supports the `no-modules` target:\r\n```sh\r\nwasm-pack build -t no-modules\r\n```\r\n\r\n### WASM in Web Worker\r\nSince the main thread in web cannot block, you must use blocking operations in a \r\nweb worker, this include:\r\n1. Calling `join` on a JoinHandle\r\n2. Calling `recv` on a Receiver in the `std::sync` library or `oneshot` library.\r\n\r\nThe [example](https://github.com/Pistonite/wasm-bindgen-spawn/tree/main/example) shows how to put the WASM module in the worker. You can\r\nthen use some kind of RPC with `postMessage` to communicate between the main thread and the worker.\r\nThis is probably something you have to do anyway to avoid the heavy, multithreaded computation freezing the UI.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpistonite%2Fwasm-bindgen-spawn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpistonite%2Fwasm-bindgen-spawn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpistonite%2Fwasm-bindgen-spawn/lists"}