Can you share WebAssembly memory between Web Workers?

怎甘沉沦 提交于 2020-02-01 06:33:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!