javascript - How to achieve parallelism for SharedArrayBuffer and Atomics? - Stack Overflow

ECMA-2017(ES8), just finalized about a month ago, introduces SharedArrayBuffer and Atomics. The link he

ECMA-2017(ES8), just finalized about a month ago, introduces SharedArrayBuffer and Atomics. The link here shows that they have been supported in some browsers.

As we know, they are meant to allow the sharing of data across threads. I wonder how this kind of parallelism is achieved in browsers and Node? Are we supposed to use Web Workers and the 'cluster' package respectively?

ECMA-2017(ES8), just finalized about a month ago, introduces SharedArrayBuffer and Atomics. The link here shows that they have been supported in some browsers.

As we know, they are meant to allow the sharing of data across threads. I wonder how this kind of parallelism is achieved in browsers and Node? Are we supposed to use Web Workers and the 'cluster' package respectively?

Share Improve this question edited Oct 3, 2018 at 20:55 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Jul 21, 2017 at 6:17 Chong Lip PhangChong Lip Phang 9,2797 gold badges75 silver badges114 bronze badges 2
  • Node is a single thread application, No? – Alexander Higgins Commented Jul 21, 2017 at 6:19
  • 1 Well, I think threads can be forked and made to run in parallel on separate cores with the 'cluster' package. – Chong Lip Phang Commented Jul 21, 2017 at 6:21
Add a ment  | 

2 Answers 2

Reset to default 4

Indeed, for browsers SharedArrayBuffers and Atomics are intended to be used with WebWorkers, which is the natural way to have code run in concurrent threads in a browser context.

For Node.js, the threads spawned with the cluster package would indeed be candidates for sharing data, but at the time of writing there is no implementation of SharedArrayBuffers in Node.js yet, so this is theory. You may want to scan several discussions about it:

  • Tracking issue: Worker support
  • Does NodeJS have any plans for shared memory?
  • High level architecture
  • Why is it so hard to add threads to nodejs? or web workers?

There already is the ems package which allows sharing data between different threads and processes.

Related:

  • node-webworker-threads - Lightweight Web Worker API implementation with native threads

For nodejs the SharedArrayBuffer And Array buffer havee been supported officially since v10.5.0 and they are directly mentioned on the documentation on https://nodejs/api/worker_threads.html:

Unlike child_process or cluster, worker_threads can share memory. They do so by transferring ArrayBuffer instances or sharing SharedArrayBuffer instances.

Shared buffer and atomics:

https://www.sitepen./blog/the-return-of-sharedarraybuffers-and-atomics/

A good article that tackle directly the shared buffer. And the use with atomic (You must look at Atomic too).

Quick snippet (from the article):

    
// Creating a shared buffer
const length = 10;
 // Get the size we want in bytes for the buffer
const size = Int32Array.BYTES_PER_ELEMENT * length;
 // Create a buffer for 10 integers
const sharedBuffer = new SharedArrayBuffer(size);
const sharedArray = new Int32Array(sharedBuffer);

Now we have a shared buffer that we can pass to a worker context and also have an integer Array that leverages that shared buffer. To pass this buffer reference to the worker:

// main.js
worker.postMessage(sharedBuffer);

This buffer allows us to create another shared array on the worker side:

// worker.js
constsharedArray = new Int32Array(m.data);

The article cover a lot of details and the use of atomic too. Check the last section too about the support in the browser! It may be outdated already! but still relevant!

Check Introducing Atomics title for a good introduction! (too long to site here).

And from MDN https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics. A link to check.

And

https://blog.logrocket./a-plete-guide-to-threads-in-node-js-4fa3898fe74f/

And this one take the whole multi-threading including the shared buffer use. With good historical background. And it tackle many many things.

Check this too:

https://stackoverflow./a/51411795/7668448

For the use of data view!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744954588a4603117.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信