microjob icon indicating copy to clipboard operation
microjob copied to clipboard

Shared Memory (SharedArrayBuffer)

Open aleks63 opened this issue 6 years ago • 5 comments

How to use one global object/array in every workers of pool?

aleks63 avatar Jun 06 '19 12:06 aleks63

I don't understand your case: can you explain it better with an example, please?

wilk avatar Jul 17 '19 06:07 wilk

I think he is asking how to use shared memory between worker processes. For example have a large global object that all the workers can manipulate.

0x7061 avatar Aug 13 '19 21:08 0x7061

Instead of opening a new issue I'd like to ask a similar question here:

Am I right to assume that if I wanted to access a global variable (for example an express app instance), I would have to use context or pass it via data - which, however, both don't support functions and classes. So basically there's currently no way to access more complex data structures from within worker threads?

0x7061 avatar Aug 13 '19 22:08 0x7061

@codepushr thanks for the clarification!

@aleks63 Sorry for the late response. The documentation of SAB says that (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer):

The structured clone algorithm accepts SharedArrayBuffers and TypedArrays mapped onto SharedArrayBuffers. In both cases, the SharedArrayBuffer object is transmitted to the receiver resulting in a new, private SharedArrayBuffer object in the receiving agent (just as for ArrayBuffer). However, the shared data block referenced by the two SharedArrayBuffer objects is the same data block, and a side effect to the block in one agent will eventually become visible in the other agent.

So, basically you can send a SAB inside the data option and treat it like a SAB as you would use in traditional worker threads, following the documentation.

@codepushr The only thing you cannot pass via context are classes: https://github.com/wilk/microjob/blob/master/GUIDE.md#job-context So, yes, you can pass functions via context.

wilk avatar Oct 19 '19 13:10 wilk

fix #51

darky avatar Oct 26 '19 11:10 darky