TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A Taste of JavaScript's New Parallel Primitives

237 pointsby faideabout 9 years ago

14 comments

pfootiabout 9 years ago
You know, I&#x27;m not entirely sure how I feel about this. On the one hand: yeah, I get that having really multithreaded stuff is pretty handy, especially for certain computationally-bound tasks.<p>On the other hand, I quite like the single-threadedness of javascript. Promises-based systems (or async&#x2F;await) give us basically cooperative multitasking anyway to break up long-running (unresponsive) threads without worrying about mutexes and semaphores. I understand exactly when and where my javascript code will be interrupted, and I don&#x27;t need to wrap blocks in atomic operation markers extraneously.<p>I&#x27;ve written plenty multithreaded code, starting with old pthreads stuff and eventually moving on to Java (but my own experience with threaded stuff is limited mainly to C and Java), and it can be a <i>real pain</i>. I guess limiting shared memory to explicitly named blocks means you don&#x27;t have as much to worry about vis-a-vis nonreentrant code messing up your memory space.<p>That said, it is a pretty useful construct, and I see where this can benefit browser-based games dev in particular (graphics can be sped up a lot with multicore rendering, I bet).
评论 #11639499 未加载
评论 #11639659 未加载
评论 #11638854 未加载
评论 #11638385 未加载
评论 #11639235 未加载
评论 #11639458 未加载
评论 #11640458 未加载
评论 #11638414 未加载
_getifyabout 9 years ago
I&#x27;m excited about the `SharedArrayBuffer` addition, but quite meh on the `Atomic.wait()` and `Atomic.wake()`.<p>I think CSP&#x27;s channel-based message control is a far better fit here, especially since CSP can quite naturally be modeled inside generators and thus have only local-blocking.<p>That means the silliness of &quot;the main thread of a web page is not allowed to call Atomics.wait&quot; becomes moot, because the main thread can do `yield CSP.take(..)` and not block the main UI thread, but still simply locally wait for an atomic operation to hand it data at completion.<p>I already have a project that implements a bridge for CSP semantics from main UI thread to other threads, including adapters for web workers, remote web socket servers, node processes, etc: <a href="https:&#x2F;&#x2F;github.com&#x2F;getify&#x2F;remote-csp-channel" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;getify&#x2F;remote-csp-channel</a><p>What&#x27;s exciting, for the web workers part in particular, is the ability to wire in SharedArrayBuffer so the data interchange across those boundaries is extremely cheap, while still maintaining the CSP take&#x2F;put semantics for atomic-operation control.
schmichaelabout 9 years ago
&gt; if we want JS applications on the web to continue to be viable alternatives to native applications on each platform<p>This is where I disagree with the direction Mozilla has been going for years. I don&#x27;t want the web to be a desktop app replacement with HTTP as the delivery mechanism. I&#x27;m fine with rich single page web apps, but I don&#x27;t understand the reason why web apps need complete feature parity with desktop apps.<p>Why not let the web be good at some things and native apps be good at others?
评论 #11639619 未加载
评论 #11639529 未加载
评论 #11639963 未加载
评论 #11639519 未加载
评论 #11646467 未加载
评论 #11639706 未加载
seibeljabout 9 years ago
This is the last piece needed to allow multi-threaded code with shared state to emscripten [0]. A very good thing indeed<p>[0] <a href="http:&#x2F;&#x2F;kripken.github.io&#x2F;emscripten-site&#x2F;docs&#x2F;porting&#x2F;guidelines&#x2F;portability_guidelines.html" rel="nofollow">http:&#x2F;&#x2F;kripken.github.io&#x2F;emscripten-site&#x2F;docs&#x2F;porting&#x2F;guidel...</a>
ryandvmabout 9 years ago
The saving grace of JavaScript&#x27;s everything-is-async, single threaded model was that it was just slightly less difficult to reason about than multiple threads and shared state. (Though I&#x27;d say that&#x27;s debatable...)<p>My guess is that, despite the sugar coating that JavaScript&#x27;s async internals have received of late, writing stable multi-threaded code with JavaScript is going to be hard.<p>JavaScript now has the safety of multi-threaded code with the ease of asynchronicity!
评论 #11638712 未加载
评论 #11639188 未加载
bhauerabout 9 years ago
On my grossly overpowered workstation, I can only crank the number of workers in the Mandelbrot demo to 20 [1]. Attempting to go beyond 20, the console reports:<p><pre><code> RangeError: out-of-range index for atomic access </code></pre> That said, 20 workers is about 11x faster than the single-threaded version.<p>[1] <a href="https:&#x2F;&#x2F;axis-of-eval.org&#x2F;blog&#x2F;mandel3.html?numWorkers=20" rel="nofollow">https:&#x2F;&#x2F;axis-of-eval.org&#x2F;blog&#x2F;mandel3.html?numWorkers=20</a>
CHsurferabout 9 years ago
I keep hoping that JS would evolve to support the actor model, a la Erlang&#x2F;Elixir, with their process based persistence, concurrency via message passing, etc. It just seems so much simpler and tractable than this proposal.
评论 #11639296 未加载
评论 #11638865 未加载
评论 #11639025 未加载
kbensonabout 9 years ago
&gt; This leads to the following situation where the main program and the worker both reference the same memory, which doesn’t belong to either of them:<p>If only Mozilla had some technology that could deal with ownership of memory...<p>Seriously, if rust doesn&#x27;t have an ASM.js optimized target yet, it really should.
评论 #11638930 未加载
评论 #11640709 未加载
评论 #11641103 未加载
评论 #11638470 未加载
rl3about 9 years ago
&gt;<i>Consider synchronization: The new Atomics object has two methods, wait and wake, which can be used to send a signal from one worker to another: one worker waits for a signal by calling Atomics.wait, and the other worker sends that signal using Atomics.wake.</i><p>Having not yet played with this myself: is anyone familiar with what kind of latency overhead is involved with signaling in the Atomics API? I&#x27;m not very familiar with the API yet, so I&#x27;ve no idea how signaling is implemented under the hood.<p>The MessageChannel API by contrast (i.e. <i>postMessage</i>) can be quite slow, depending. While you can use it within a render loop, it usually pays to be very sparing with it. Typical latency for a virtually empty <i>postMessage</i> call on an already-established channel is usually .05ms to .1ms. Most serialization operations will usually balloon that to well over 1ms (hence the need for shared memory). Plus transferables suck.<p>&gt;<i>Finally, there is clutter that stems from shared memory being a flat array of integer values; more complicated data structures in shared memory must be managed manually.</i><p>This is probably the biggest drawback to the API, at least for plain Javascript. It really favors asm.js or WebAssembly compile targets for seamless operation, whereas plain Javascript can&#x27;t even share native types without serialization&#x2F;deserialization operations to and from byte arrays.
kgrabout 9 years ago
I&#x27;m excited to see progress in the area of JS concurrency, but I&#x27;m not sure how useful this is going to be. It lets me share ArrayBuffers between workers, but all of my data is in the form of Objects, not primitive arrays.<p>One place where I would like to use this is for collision detection, like in this example: <a href="http:&#x2F;&#x2F;codepen.io&#x2F;kgr&#x2F;pen&#x2F;GoeeQw" rel="nofollow">http:&#x2F;&#x2F;codepen.io&#x2F;kgr&#x2F;pen&#x2F;GoeeQw</a><p>But I&#x27;m relying on objects with polymorphic intersects() methods to determine if they intersect with each other, and once I encode everything up as arrays, I lose the convenience and power of objects.
评论 #11638698 未加载
hkjgkjyabout 9 years ago
If only we did not have mutable data structures, there would be no or few problems to find in this.<p>Concurrency isn&#x27;t hard - try Clojure core&#x2F;async and you will find out. Shared mutable state is mind-boggingly hard
piotrkaminskiabout 9 years ago
If the problem that this is trying to solve is that `postMessage` is slow and you can&#x27;t transfer slices of arrays, then perhaps they should solve it by speeding up `postMessage` and making array slicing cheap? Forcing a shared-memory concurrency model into JavaScript seems like a bit of an overreaction.
cpetersoabout 9 years ago
Are JavaScript workers implemented using real OS threads or green threads? How heavyweight is a worker?
评论 #11639693 未加载
imaginenoreabout 9 years ago
I really want WebCL. Worker threads are just so lame compared to what GPUs can do.