Function web_thread::web::has_spawn_support
source · pub fn has_spawn_support() -> bool
Available on
Web
only.Expand description
Returns true
if the main thread supports spawning threads.
§Notes
web-thread
will consider the first thread it finds itself in the
“main thread”. If Wasm is instantiated in a dedicated worker, it will
consider it as the “main thread”.
Currently only two thread types are known to support spawning threads:
Window
(often called the main thread on Web).- Dedicated worker.
Additionally, the following is required to allow spawning threads:
- The atomics target feature is enabled.
- The site needs to be cross-origin isolated.
§Example
fn schedule_fun(f: impl 'static + FnOnce() + Send) {
if web_thread::web::has_spawn_support() {
web_thread::spawn(f);
} else {
wasm_bindgen_futures::spawn_local(async { f() });
}
}
schedule_fun(|| web_sys::console::log_1(&"Are we having fun yet?".into()));