9 ms·
If you allow threads to share memory arbitrarily you need to add locking to all internal VM structures, which is going to be a significant slowdown. Also it's
by devit 11y ago
If you allow threads to share memory arbitrarily you need to add locking to all internal VM structures, which is going to be a significant slowdown.
Also it's not (any longer) considered good practice to have languages that allow mutable memory sharing since that makes software unreliable, so it's not really a good idea.
Without arbitrary memory sharing, multi-threading is already supported with web workers.
- netheril96 11y agoWeb Worker is very weird in that you have to have a separate file.
- angersock 11y agoNot with a little imagination and hackishness! http://stackoverflow.com/a/10372280 http://stackoverflow.com/a/10372280
- padolsey 11y agoAgreed! FWIW this is why I made Operative. It gives you a way of writing "inline" JS that utilizes web workers (caveat: not actually inline; no scope/context access of course). It provides good support across browsers and fallbacks for envs where web workers don't exist (and ~all the in-between cases): https://github.com/padolsey/operative https://github.com/padolsey/operative
- chadscira 11y agoLooks nice! I took a stab at this a while back https://gist.github.com/icodeforlove/deb0f19a9e7bd528bd48 https://gist.github.com/icodeforlove/deb0f19a9e7bd528bd48
- DiThi 11y agoSharing only typed arrays shouldn't be hard.
- angersock 11y agoShared memory in Javascript is a Bad Idea(tm). Run, don't walk, to your standards body, and tell them not to entertain such notions. You know what happens with shared typed arrays? Shared Uint8 arrays. You know what happens with shared Uint8 arrays? Multiple workers using JSON.parse. Do you want ants? Because this is how we get ants. :(
- DiThi 11y agoWhy would anybody ever want to JSON.parse typed arrays from web workers when the parsed data (or unparsed strings) can be passed around directly? Strings are immutable and are not copied when you pass them around. I don't see your point.
- angersock 11y agoRemember: web workers communicate (to the best of my knowledge) using a full serialization/deserialization of message objects (which is why they added transferable objects). So, to create a one-to-many broadcast mechanism, you'd do exactly what I described. Additionally, I am unsure, but I could easily see immutable strings being copied nonetheless between web workers because of the desire to give every worker a private heap.
- DiThi 11y agoI still think is faster to send the same message to all than using JSON.parse on each (not to mention much more compatible). Since each worker is going to make a copy anyway (with JSON.parse or by receiving a message), why does it matter? Also, assuming what you say is true, what's the problem? It's much easier for the JS implementations to synchronize things only with a specific type of typed arrays than sharing all kinds of GC-managed data structures.
- Klathmon 11y agoWell it's not sharing, but web workers have a zero-copy way of transferring typed arrays between web workers. [1] It gives many of the benefits of sharing memory without all of the gotchas [1]https://developer.mozilla.org/en-US/docs/Web/API/Transferable https://developer.mozilla.org/en-US/docs/Web/API/Transferabl...