wasm_thread

An std::thread replacement for wasm32 target.

This crate tries to closely replicate std::thread API, however, some features are not yet implemented or are even impossible to implement.

Running examples

Native

wasm32

Using as a library

Notes on wasm limitations

Example output

Native: hi number 1 from the spawned thread ThreadId(2)! hi number 1 from the main thread ThreadId(1)! hi number 1 from the spawned thread ThreadId(3)! hi number 2 from the main thread ThreadId(1)! hi number 2 from the spawned thread ThreadId(2)! hi number 2 from the spawned thread ThreadId(3)!

Wasm: hi number 1 from the main thread ThreadId(1)! hi number 2 from the main thread ThreadId(1)! hi number 1 from the spawned thread ThreadId(2)! hi number 1 from the spawned thread ThreadId(3)! hi number 2 from the spawned thread ThreadId(2)! hi number 2 from the spawned thread ThreadId(3)!

As you can see wasm threads are only spawned after main() returns, because browser event loop cannot continue while main thread is blocked.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.