Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sure, async/await can have arbitrary side effects, just like functions, and threads can be used to return values - but the design pattern nudges you to a particular end. Basically the async/await version of some code looks like this:

  f = await getAsync();
While the thread-based version looks like this:

  var f;
  t = startThread(getAsync(&f));
  t.join();
 
This is a fundamentally different API. Of course you can implement one with the other, but that doesn't make them the same thing (just like objects and closures can be used to implement each other, but they are different nonetheless).

Of course, there are other concurrency APIs as well, such as the C++ futures you show. Those have other advantages, disadvantages, and workflows for which they fit best. The main difference is that get() on the future blocks the current OS thread if the value isn't available yet, while await doesn't. Thus, futures aren't very well suited to running many concurrent IO operations on the same thread.



You never "have" to await at the call site. There's very little stopping you from:

  fFuture = getAsync();
  // do other things
  f = await fFuture;
Most implementations of async/await Future/Task/Promise often have some sort of `all` combinator to multi-join as soon as all results arrive, even:

  fFuture = getFAsync();
  gFuture = getGAsync();
  hFuture = getHAsync();
  // other stuff
  (f, g, h) = await all(fFuture, gFuture, hFuture);
The syntax makes it really easy to do the simple step-by-step await at every call site, but it also doesn't stop you from writing more complex things. (Sometimes very much more complex things when you get into combinators like `all` and `race`.)


Oh, yes, I didn't think to mention that since I was focused on showing what pattern the syntax makes easiest.


To be 100% clear: the std::async example I gave uses threads. Don't confuse a specific OO API with the general concept. Even vintage pthreads allow returning values on thread join.

And of course you would run multiple concurrent operations on separate threads.

edit: and of course futures are completely orthogonal to threads vs async/await. You can use them with either.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: