wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

Shared Memory (SharedArrayBuffer) #42

Open aleks63 opened 5 years ago

aleks63 commented 5 years ago

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

wilk commented 5 years ago

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

codepushr commented 5 years ago

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.

codepushr commented 5 years ago

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?

wilk commented 4 years ago

@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.

darky commented 4 years ago

fix #51