Link: https://github.com/rustwasm/wasm-bindgen/blob/main/examples/raytrace-parallel/src/pool.rs#L119
Signature:
fn execute(&self, f: impl FnOnce() + Send + 'static) -> Result<Worker, JsValue> {
Context: this is part of a small 'library' which allows rayon style parallelism with webworkers.
Question:
Do these web workers share the same memory ?
If not, how is passing around closures work?
If so, how is the memory kept safe in light of multiple threads? All worker threads are limited to the same 2GB / 4GB of memory ?
As you can see, this method does the following:
boxes the passed closure;
converts the box to raw pointer;
sends the raw pointer to worker via post_message
.
So, it seems that the memory is shared, since otherwise the pointer would be dangling after sending. And, indeed, when spawning a new worker thread, wasm_bindgen::memory
is being sent there, which is the handler to global memory allocation .
1 Like
system
Closed
August 26, 2022, 10:40am
3
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.