wasm-mt icon indicating copy to clipboard operation
wasm-mt copied to clipboard

Any plan to support threads/SharedArrayBuffer?

Open Pistonight opened this issue 5 months ago • 2 comments

threads proposal has reached phase 4 - https://webassembly.org/features/

Seems like it would be possible to build a wrapper for something like std::thread::spawn that, under the hood, sends the closure to another worker (possibly by boxing and maybe needs to turn it into a raw pointer)


pub fn spawn_thread<T, F>(f: F) -> JoinHandle<T> where F: FnOnce() -> T + Send +'static, T: Send + 'static {
    // (std/crossbeam should work, maybe one_shot works too?)
    let (send, recv) = std::sync::mpsc::channel();
    let (out_send, out_recv) = std::sync::mpsc::channel();

    // create a worker with js_sys that:
    // - init the wasm module (produced by wasm-bindgen) inside the worker with the same (shared) WebAssembly.Memory
    // - recv and execute the closure
    // - send the result back

    // send the closure to the worker
    send.send(f).unwrap()
    JoinHandle { recv: out_recv }
}

(if this won't work please educate me why)

Pistonight avatar Oct 01 '24 19:10 Pistonight