Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
Is passing Int32Array instead of SharedArrayBuffer supposed to work?

I have some code that creates a shared array buffer and passes the Int32Array and NOT the SharedArrayBuffer to the worker:

let worker = new Worker(...);
worker.postMessage({
  position: {
    x: createArray(),
    y: createArray()
  },
  velocity: {
    x: createArray(),
    y: createArray()
  }
});

function createArray(size = 1024) {
  let buffer = new SharedArrayBuffer(size);
  return new Int32Array(buffer);
}

In the worker I'm updating the resulting integers in a loop across the array.

let components = e.data;

let velocityX = Atomics.load(components.velocity.x, shipEid);
let velocityY = Atomics.load(components.velocity.y, shipEid);
Atomics.add(components.position.x, shipEid, velocityX);
Atomics.add(components.position.y, shipEid, velocityY);

Now, my question is, is it valid to pass the Int32Array to the worker instead of the SharedArrayBuffer? I honestly didn't even realize I was doing this until much later, and now that I realize it I'm wondering why it works. I thought you had to pass the SharedArrayBuffer to the worker in order to share the memory between the worker and the main process. Is this actually part of the spec to work this way, or is this something Chrome specific that will break for other browsers and possibly break in Chrome in the future?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, this is valid. Notice that you're not actually passing the Int32Array either: you're passing an object which has properties that are objects which have properties that are typed arrays. This works because the argument to postMessage is actually structurally cloned, and when the cloning algorithm reaches the SharedArrayBuffer that is part of your structure then it is cloned into a new buffer object that shares the underlying memory.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda