How to achieve parallelism for SharedArrayBuffer and Atomics?

Viewed 2607

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?

2 Answers

For nodejs the SharedArrayBuffer And Array buffer are supported officially! And they are directly mentioned on the documentation https://nodejs.org/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.com/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.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics. A link to check.

And

https://blog.logrocket.com/a-complete-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.com/a/51411795/7668448

For the use of data view!

Related