问题
Is it possible to share WebAssembly.memory between 2 workers by using postMessage, something like SharedArrayBuffer? And if the answer is yes, how?
回答1:
You can create a WebAssembly shared memory instance via the JavaScript API:
const memory = new WebAssembly.Memory({
initial: 80,
maximum: 80,
shared: true
});
You can then send this memory instance to a Web Worker via postMessage
:
const worker = new Worker("worker.js");
worker.postMessage({ memory });
The file worker.js
can then create a WebAssembly module using this shared memory instance, allowing it to be shared across module instance in different threads.
For a more complete example, see this blog post:
Faster Fractals with Multi-Threaded WebAssembly
来源:https://stackoverflow.com/questions/59818702/can-you-share-webassembly-memory-between-web-workers