1

In the features of ECMAScript 2017 there is Shared memory and atomics. I try test it in Google Chrome 59.0.3071.109 (Experimental SharedArrayBuffer flag is enabled). When I try post SharedArrayBuffer in simple Worker it's works. But when I try post SharedArrayBuffer in SharedWorker, in event parament of onmessage event handler I get event.data is null. Why is this so? Here is an example of my code:

index.html:

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Web Workers</title>
    <script src="main.js"></script>
</head>

<body>

<button onclick="post()">Post</button>
<button onclick="get()">Get</button>

</body>
</html>

main.js:

var worker = new SharedWorker('worker.js');
var buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
new Int32Array(buffer)[0] = 12;

function post() {
    worker.port.postMessage({buffer});
}

function get() {
    console.log(new Int32Array(buffer)[0]);
}

worker.js:

self.onconnect = function (e) {
    var [port] = e.ports;

    port.onmessage = function (e) {
        console.log(e.data);// null
    };
};

Thank you.

5
  • Not sure how can you get that result but as far as I know, SharedArrayBuffer is still not yet supported on Chrome 59. Commented Jun 25, 2017 at 9:33
  • There is chrome://flags/#shared-array-buffer. I will repeat that everything is working with simple worker. Commented Jun 25, 2017 at 9:40
  • kangax.github.io/compat-table/es2016plus Here it is also indicated. [17] The feature have to be enabled via "Experimental enabled SharedArrayBuffer support in JavaScript." setting under about:flags Commented Jun 25, 2017 at 9:43
  • So there's reasons that this feature is still behind a flag. It's not yet stable. In this case, I think there's a bug with the current implementation on Chrome. Commented Jun 25, 2017 at 9:55
  • Ok. May be. When asking a question, I tried to understand this bug or feature. Thank you for your version. Commented Jun 25, 2017 at 10:40

1 Answer 1

2

According Chromium Blink Workers Documention: Shared Worker is Out-of-process.

It run on another system process different to document process(which contain JS mainthread).

Operation System process deny memory access from other process.

SharedArraybuffer work by share(access) memory. So SharedArraybuffer create(access) by JS mainthread(document process) can't access by Shared Worker(Out-of-process).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.