From 1ddb975d4648c4b505c8a8b332d577db4327c277 Mon Sep 17 00:00:00 2001 From: Sahil Jain Date: Tue, 2 Aug 2016 14:55:16 -0700 Subject: [PATCH] Update folly/futures README Summary: Regenerate README after updating dex_export.php Reviewed By: fugalh Differential Revision: D3654216 fbshipit-source-id: aaae04803c480e2ecaf9b3cb8794c8d8f7df560b --- folly/futures/README.md | 543 +++++++++++++++++++++------------------- 1 file changed, 283 insertions(+), 260 deletions(-) diff --git a/folly/futures/README.md b/folly/futures/README.md index 1af0945d..d25649d5 100644 --- a/folly/futures/README.md +++ b/folly/futures/README.md @@ -46,9 +46,9 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

Let's begin with an example using an imaginary simplified Memcache client interface:

-
using std::string;
-class MemcacheClient {
- public:
+
using std::string;
+class MemcacheClient {
+ public:
   struct GetReply {
     enum class Result {
       FOUND,
@@ -56,46 +56,46 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac
       SERVER_ERROR,
     };
 
-    Result result;
+    Result result;
     // The value when result is FOUND,
     // The error message when result is SERVER_ERROR or CLIENT_ERROR
     // undefined otherwise
-    string value;
-  };
+    string value;
+  };
 
-  GetReply get(string key);
-};
-
+
GetReply get(string key); +}; +

This API is synchronous, i.e. when you call get() you have to wait for the result. This is very simple, but unfortunately it is also very easy to write very slow code using synchronous APIs.

Now, consider this traditional asynchronous signature for get():

-
int get(string key, std::function<void(GetReply)> callback);
-
+
int get(string key, std::function<void(GetReply)> callback);
+

When you call get(), your asynchronous operation begins and when it finishes your callback will be called with the result. Very performant code can be written with an API like this, but for nontrivial applications the code devolves into a special kind of spaghetti code affectionately referred to as "callback hell".

The Future-based API looks like this:

-
Future<GetReply> get(string key);
-
+
Future<GetReply> get(string key);
+

A Future<GetReply> is a placeholder for the GetReply that we will eventually get. A Future usually starts life out "unfulfilled", or incomplete, i.e.:

-
fut.isReady() == false
+
fut.isReady() == false
 fut.value()  // will throw an exception because the Future is not ready
 

At some point in the future, the Future will have been fulfilled, and we can access its value.

-
fut.isReady() == true
+
fut.isReady() == true
 GetReply& reply = fut.value();
-
+

Futures support exceptions. If something exceptional happened, your Future may represent an exception instead of a value. In that case:

-
fut.isReady() == true
+
fut.isReady() == true
 fut.value() // will rethrow the exception
 
@@ -105,40 +105,40 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

First, we can aggregate Futures, to define a new Future that completes after some or all of the aggregated Futures complete. Consider two examples: fetching a batch of requests and waiting for all of them, and fetching a group of requests and waiting for only one of them.

-
vector<Future<GetReply>> futs;
-for (auto& key : keys) {
+
vector<Future<GetReply>> futs;
+for (auto& key : keys) {
   futs.push_back(mc.get(key));
-}
+}
 auto all = collectAll(futs.begin(), futs.end());
 
-vector<Future<GetReply>> futs;
-for (auto& key : keys) {
+vector<Future<GetReply>> futs;
+for (auto& key : keys) {
   futs.push_back(mc.get(key));
-}
+}
 auto any = collectAny(futs.begin(), futs.end());
-
+

all and any are Futures (for the exact type and usage see the header files). They will be complete when all/one of futs are complete, respectively. (There is also collectN() for when you need some.)

Second, we can attach callbacks to a Future, and chain them together monadically. An example will clarify:

-
Future<GetReply> fut1 = mc.get("foo");
+
Future<GetReply> fut1 = mc.get("foo");
 
-Future<string> fut2 = fut1.then(
+Future<string> fut2 = fut1.then(
   [](GetReply reply) {
     if (reply.result == MemcacheClient::GetReply::Result::FOUND)
       return reply.value;
-    throw SomeException("No value");
-  });
+    throw SomeException("No value");
+  });
 
-Future<Unit> fut3 = fut2
+Future<Unit> fut3 = fut2
   .then([](string str) {
     cout << str << endl;
-  })
+  })
   .onError([](std::exception const& e) {
     cerr << e.what() << endl;
-  });
-
+
}); +

That example is a little contrived but the idea is that you can transform a result from one type to another, potentially in a chain, and unhandled errors propagate. Of course, the intermediate variables are optional.

@@ -149,15 +149,15 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

Futures are partially threadsafe. A Promise or Future can migrate between threads as long as there's a full memory barrier of some sort. Future::then and Promise::setValue (and all variants that boil down to those two calls) can be called from different threads. But, be warned that you might be surprised about which thread your callback executes on. Let's consider an example.

// Thread A
-Promise<Unit> p;
-auto f = p.getFuture();
+Promise<Unit> p;
+auto f = p.getFuture();
 
 // Thread B
-f.then(x).then(y).then(z);
+f.then(x).then(y).then(z);
 
 // Thread A
-p.setValue();
-
+
p.setValue(); +

This is legal and technically threadsafe. However, it is important to realize that you do not know in which thread x, y, and/or z will execute. Maybe they will execute in Thread A when p.setValue() is called. Or, maybe they will execute in Thread B when f.then is called. Or, maybe x will execute in Thread B, but y and/or z will execute in Thread A. There's a race between setValue and then—whichever runs last will execute the callback. The only guarantee is that one of them will run the callback.

@@ -165,77 +165,77 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

The first and most useful is via, which passes execution through an Executor, which usually has the effect of running the callback in a new thread.

-
aFuture
+
aFuture
   .then(x)
   .via(e1).then(y1).then(y2)
   .via(e2).then(z);
-
+

x will execute in the current thread. y1 and y2 will execute in the thread on the other side of e1, and z will execute in the thread on the other side of e2. If after z you want to get back to the current thread, you need to get there via an executor. Another way to express this is using an overload of then that takes an Executor:

-
aFuture
+
aFuture
   .then(x)
   .then(e1, y1, y2)
   .then(e2, z);
-
+

Either way, there is no ambiguity about which thread will execute y1, y2, or z.

You can still have a race after via if you break it into multiple statements, e.g. in this counterexample:

-
f = f.via(e1).then(y1).then(y2); // nothing racy here
-f2.then(y3); // racy
-
+
f = f.via(e1).then(y1).then(y2); // nothing racy here
+f2.then(y3); // racy
+

You make me Promises, Promises #

If you are wrapping an asynchronous operation, or providing an asynchronous API to users, then you will want to make Promises. Every Future has a corresponding Promise (except Futures that spring into existence already completed, with makeFuture()). Promises are simple: you make one, you extract the Future, and you fulfill it with a value or an exception. Example:

-
Promise<int> p;
-Future<int> f = p.getFuture();
+
Promise<int> p;
+Future<int> f = p.getFuture();
 
-f.isReady() == false
+f.isReady() == false
 
 p.setValue(42);
 
-f.isReady() == true
+f.isReady() == true
 f.value() == 42
 

and an exception example:

-
Promise<int> p;
-Future<int> f = p.getFuture();
+
Promise<int> p;
+Future<int> f = p.getFuture();
 
-f.isReady() == false
+f.isReady() == false
 
 p.setException(std::runtime_error("Fail"));
 
-f.isReady() == true
+f.isReady() == true
 f.value() // throws the exception
 

It's good practice to use setWith which takes a function and automatically captures exceptions, e.g.

-
Promise<int> p;
-p.setWith([]{
-  try {
+
Promise<int> p;
+p.setWith([]{
+  try {
     // do stuff that may throw
     return 42;
-  } catch (MySpecialException const& e) {
+  } catch (MySpecialException const& e) {
     // handle it
     return 7;
-  }
+  }
   // Any exceptions that we didn't catch, will be caught for us
 });
-

More Details

Let's look at a contrived and synchronous example of Futures.

+

More Details

Let's look at a contrived and synchronous example of Futures.

-
Promise<int> p;
-Future<int> f = p.getFuture();
+
Promise<int> p;
+Future<int> f = p.getFuture();
 // ...
-p.setValue(42); // or setException(...)
-cout << f.value(); // prints 42
-
+
p.setValue(42); // or setException(...) +cout << f.value(); // prints 42 +

First, we create a Promise object of type int. This object is exactly what it sounds like—a pledge to provide an int (or an exception) at some point in the future.

@@ -247,54 +247,54 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

Ok, great, so now you're wondering what these are actually useful for. Let's consider another way to follow up on the result of a Future once its corresponding Promise is fulfilled—callbacks! Here's a snippet that is functionally equivalent to the one above:

-
Promise<int> p;
-Future<int> f = p.getFuture();
+
Promise<int> p;
+Future<int> f = p.getFuture();
 
-f.then([](int i){
+f.then([](int i){
   cout << i;
-});
+});
 
-p.setValue(42);
-
+
p.setValue(42); +

That then() method on futures is the real bread and butter of Futures code. It allows you to provide a callback which will be executed when that Future is complete. Note that while we fulfill the promise after setting the callback here, those operations could be swapped. Setting a callback on an already completed future executes the callback immediately.

In this case, the callback takes a value directly. If the Future contained an exception, the callback will be passed over and the exception will be propagated to the resultant Future - more on that in a second. Your callback may also take a Try, which encapsulates either an exception or a value of its templated type.

-
f.then([](Try<int> const& t){
+
f.then([](Try<int> const& t){
   cout << t.value();
-});
-
+
}); +
-
NOTE: Do not use Try unless you are actually going to do exception handling in your callback. It is much cleaner and often more performant to take the value directly when you can. If you want to do exception handling, there still might be better options than Try. See Error Handling.
+
NOTE: Do not use Try unless you are actually going to do exception handling in your callback. It is much cleaner and often more performant to take the value directly when you can. If you want to do exception handling, there still might be better options than Try. See Error Handling.
NOTE: When passing a callback to then, the future stores a copy of it until the callback has been executed. If, for example, you pass a lambda function that captures a shared pointer, the future will keep the referenced object alive only until the callback has been executed.

The real power of then() is that it returns a Future of the type that the callback returns and can therefore be chained and nested with ease. Let's consider another example:

-
Future<string> f2 = f.then([](int i){
+
Future<string> f2 = f.then([](int i){
   return folly::to<string>(i);
-});
+});
 
-f2.then([](string s){ /* ... */ });
-
+
f2.then([](string s){ /* ... */ }); +

Here, we convert that int to a string in the callback and return the result, which results in a Future<string> that we can set further callbacks on. I've created a named variable f2 to demonstrate types but don't hesitate to chain futures directly:

-
auto finalFuture = getSomeFuture()
+
auto finalFuture = getSomeFuture()
   .then(...)
   .then(...)
   .then(...);
-
+

That's all great, but this code is still synchronous. These constructs truly become useful when you start to chain, nest, and compose asynchronous operations. Let's say you instead have some remote service that converts your integers to strings for you, and that you also have a client with Future interfaces (i.e. interfaces that return Futures). Now let's leverage the fact that then() also allows you to return Future<T> from inside your callbacks as well as just T:

-
Future<string> f2 = f.then([](int i){
+
Future<string> f2 = f.then([](int i){
   return getClient()->future_intToString(i); // returns Future<string>
-});
+});
 
-f2.then([](Try<string> const& s){ ... });
-
+
f2.then([](Try<string> const& s){ ... }); +

In general, your code will be cleaner if you return T from your callbacks and only switch to returning Future<T> when necessary (i.e. when there is a nested call to a future-returning function).

@@ -307,13 +307,13 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

Synchronously entering and exiting the futures paradigm can be useful, especially in tests, so the following utilities are available:

    -
  • Create already-completed futures with makeFuture<T>(), which takes a T&& (or an exception, more info here). If you pass T&& the type is inferred and you don't have to specify it.
  • -
  • Extract a future's T value with Future<T>::get(). This method is blocking, so make sure that either your future is already completed or that another thread will complete the future while the calling thread blocks. get() can also take a timeout—see Timeouts.
  • -
  • Perform a blocking wait on a Future with Future<T>::wait(). This is just like get() but it instead of extracting the value or throwing the exception, wait() returns a new Future with the result of the input Future. Like get(), wait() can also take a timeout—see Timeouts.
  • -
  • getVia() and waitVia(), which are like get() and wait() except that they drive some Executor (say, an EventBase) until the Future is complete. See Testing for more.
  • +
  • Create already-completed futures with makeFuture<T>(), which takes a T&& (or an exception, more info here). If you pass T&& the type is inferred and you don't have to specify it.
  • +
  • Extract a future's T value with Future<T>::get(). This method is blocking, so make sure that either your future is already completed or that another thread will complete the future while the calling thread blocks. get() can also take a timeout—see Timeouts.
  • +
  • Perform a blocking wait on a Future with Future<T>::wait(). This is just like get() but it instead of extracting the value or throwing the exception, wait() returns a new Future with the result of the input Future. Like get(), wait() can also take a timeout—see Timeouts.
  • +
  • getVia() and waitVia(), which are like get() and wait() except that they drive some Executor (say, an EventBase) until the Future is complete. See Testing for more.
-
NOTE: makeFuture(), get(), wait(), and friends are especially handy in tests and are documented further in the Testing article.
+
NOTE: makeFuture(), get(), wait(), and friends are especially handy in tests and are documented further in the Testing article.

Overloads of then() #

@@ -328,44 +328,44 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

The flexibility doesn't end there. There are also overloads so that you can bind global functions, member functions, and static member functions to then():

-
void globalFunction(Try<int> const& t);
+
void globalFunction(Try<int> const& t);
 
-struct Foo {
+struct Foo {
   void memberMethod(Try<int> const& t);
-  static void staticMemberMethod(Try<int> const& t);
-};
-Foo foo;
+  static void staticMemberMethod(Try<int> const& t);
+};
+Foo foo;
 
 // bind global function
-makeFuture<int>(1).then(globalFunction);
+makeFuture<int>(1).then(globalFunction);
 // bind member method
-makeFuture<int>(2).then(&Foo::memberMethod, &foo);
+makeFuture<int>(2).then(&Foo::memberMethod, &foo);
 // bind static member method
-makeFuture<int>(3).then(&Foo::staticMemberMethod);
-
+
makeFuture<int>(3).then(&Foo::staticMemberMethod); +

A note on Promises #

Generally speaking, the majority of your futures-based code will deal with Futures alone and not Promises—calling Future-returning interfaces, composing callbacks on them, and eventually returning another Future. Promises are most useful when you're wrapping some lower level asynchronous interface so that you can return a Future:

-
void fooOldFashioned(int arg, std::function<int(int)> callback);
+
void fooOldFashioned(int arg, std::function<int(int)> callback);
 
-Future<int> foo(int arg) {
+Future<int> foo(int arg) {
   auto promise = std::make_shared<Promise<int>>();
 
-  fooOldFashioned(arg, [promise](int result) {
+  fooOldFashioned(arg, [promise](int result) {
     promise->setValue(result);
-  });
+  });
 
-  return promise->getFuture();
-}
+  return promise->getFuture();
+}
 

Though not a hard-and-fast rule, using promises heavily in your code might indicate

SharedPromise #

@@ -374,23 +374,23 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

There are several ways to introduce exceptions into your Futures flow. First, makeFuture<T>() and Promise<T>::setException() can create a failed future from any std::exception, from a folly::exception_wrapper, or from an std::exception_ptr (deprecated):

-
makeFuture<int>(std::runtime_error("oh no!"));
-makeFuture<int>(folly::make_exception_wrapper<std::runtime_error>("oh no!"));
-makeFuture<int>(std::current_exception());
+
makeFuture<int>(std::runtime_error("oh no!"));
+makeFuture<int>(folly::make_exception_wrapper<std::runtime_error>("oh no!"));
+makeFuture<int>(std::current_exception());
 
-Promise<int> p1, p2, p3;
-p1.setException(std::runtime_error("oh no!"));
-p2.setException(folly::make_exception_wrapper<std::runtime_error>("oh no!"));
-p3.setException(std::current_exception());
+Promise<int> p1, p2, p3;
+p1.setException(std::runtime_error("oh no!"));
+p2.setException(folly::make_exception_wrapper<std::runtime_error>("oh no!"));
+p3.setException(std::current_exception());
 
-
+

In general, any time you pass a function to a method that returns a Future or fulfills a Promise, you can rest assured that any thrown exceptions (including non-std::exceptions) will be caught and stored. For instance,

-
auto f = makeFuture().then([]{
+
auto f = makeFuture().then([]{
   throw std::runtime_error("ugh");
-});
-
+
}); +

is perfectly valid code. The exception will be caught and stored in the resultant Future.

@@ -411,31 +411,31 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

First, there's the Try abstraction which multiplexes values and exceptions so they can be handled simultaneously in a then() callback:

-
makeFuture<int>(std::runtime_error("ugh")).then([](Try<int> t){
-  try {
+
makeFuture<int>(std::runtime_error("ugh")).then([](Try<int> t){
+  try {
     auto i = t.value(); // will rethrow
     // handle success
-  } catch (const std::exception& e) {
+  } catch (const std::exception& e) {
     // handle failure
   }
 });
 
 // Try is also integrated with exception_wrapper
-makeFuture<int>(std::runtime_error("ugh")).then([](Try<int> t){
+makeFuture<int>(std::runtime_error("ugh")).then([](Try<int> t){
   if (t.hasException<std::exception>()) {
     // this is enough if we only care whether the given exception is present
   }
 });
 
-makeFuture<int>(std::runtime_error("ugh")).then([](Try<int> t){
+makeFuture<int>(std::runtime_error("ugh")).then([](Try<int> t){
   // we can also extract and handle the exception object
   // TODO(jsedgwick) infer exception type from the type of the function
   bool caught = t.withException<std::exception>([](const std::exception& e){
     // do something with e
   });
-});
+});
 
-
+

Unfortunately, Try encourages both intertwining success and error logic as well as excessive rethrowing. Thankfully, there's another option.

@@ -445,72 +445,72 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac
WARNING: Chaining together multiple calls to onError will NOT necessarily behave in the same way as multiple catch {} blocks after a try. Namely, if you throw an exception in one call to onError, the next onError will catch it.
-
intGeneratorThatMaybeThrows() // returns Future<int>
+
intGeneratorThatMaybeThrows() // returns Future<int>
   // This is a good opportunity to use the plain value (no Try)
   // variant of then()
   .then([](int i) { 
     return 10 * i; // maybe we throw here instead
-  })
+  })
   .onError([](const std::runtime_error& e) {
     // ... runtime_error handling ...
     return -1;
-  })
+  })
   .onError([](const std::exception& e) {
     // ... all other exception handling ...
     return -2;
-  });
-
+
}); +

You can also use onError() directly with exception_wrapper. One use case for this variant is if you want to handle non-std::exception exceptions.

-
makeFuture().then([]{
+
makeFuture().then([]{
   throw 42;
-})
+})
 .onError([](exception_wrapper ew){
   // ...
 });
-
+

ensure() #

Future<T>::ensure(F func) is similar to the finally block in languages like Java. That is, it takes a void function and will execute regardless of whether the Future contains a value or an exception. The resultant Future will contain the exception/value of the original Future, unless the function provided to ensure throws, in which case that exception will be caught and propagated instead. For instance:

-
auto fd = open(...);
-auto f = makeFuture().then([fd]{
+
auto fd = open(...);
+auto f = makeFuture().then([fd]{
   // do some stuff with the file descriptor
   // maybe we throw, maybe we don't
 })
 .ensure([fd]{
   // either way, let's release that fd
   close(fd);
-});
+});
 
 // f now contains the result of the then() callback, unless the ensure()
 // callback threw, in which case f will contain that exception
-
+

Performant Exception Handling #

Under the hood, the Futures use folly::exception_wrapper to store exceptions in a way that minimizes costly rethrows. However, the effectiveness of this mechanism depends on whether exceptions are supplied in a way that enables our library (and exception_wrapper) to maintain type information about your exception. Practically speaking, this means constructing exceptional futures directly instead of throwing. For instance:

// This version will throw the exception twice
-makeFuture()
+makeFuture()
   .then([]{
     throw std::runtime_error("ugh");
-  })
+  })
   .onError([](const std::runtime_error& e){
     // ...
   });
 // This version won't throw at all!
-makeFuture()
+makeFuture()
   .then([]{
     // This will properly wrap the exception
     return makeFuture<Unit>(std::runtime_error("ugh"));
-  })
+  })
   .onError([](const std::runtime_error& e){
     // ...
   });
-
+

Likewise, using onError instead of throwing via Try will often reduce rethrows. If you want to use Try, look at Try<T>::hasException() and Try<T>::withException() for ways to inspect and handle exceptions without rethrows.

@@ -520,19 +520,19 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

collectAll() takes an iterable collection of Future<T>s (or start and end iterators on such a collection) and returns a Future<std::vector<Try<T>>> that will complete once all of the input futures complete. The resultant Future's vector will contain the results of each in the same order in which they were passed. Errors in any component Future will not cause early termination. Input Futures are moved in and are no longer valid. For example, we could fan out and fan in a bunch of RPCs like so:

-
Future<T> someRPC(int i);
+
Future<T> someRPC(int i);
 
-std::vector<Future<T>> fs;
-for (int i = 0; i < 10; i++) {
+std::vector<Future<T>> fs;
+for (int i = 0; i < 10; i++) {
   fs.push_back(someRPC(i));
-}
+}
 
 collectAll(fs).then([](const std::vector<Try<T>>& tries){
-  for (const auto& t : tries) {
+  for (const auto& t : tries) {
     // handle each response
   }
 });
-
+
NOTE: Just as with any then() callback, you could take a Try instead and it would compile. But you shouldn't, because the only way the outer Future can fail is if there's a bug in our library. Save yourself some typing and skip the Try. This advice also applies to all of the compositional operations below whose Future types contain inner Trys (i.e. everything except for collect() and map()).
@@ -540,21 +540,21 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

There is also a variadically templated flavor of collectAll() that allows you to mix and match different types of Futures. It returns a Future<std::tuple<Try<T1>, Try<T2>, ...>>. For example:

-
Future<int> f1 = ...;
-Future<string> f2 = ...;
-collectAll(f1, f2).then([](const std::tuple<Try<int>, Try<string>>& tup) {
+
Future<int> f1 = ...;
+Future<string> f2 = ...;
+collectAll(f1, f2).then([](const std::tuple<Try<int>, Try<string>>& tup) {
   int i = std::get<0>(tup).value();
-  string s = std::get<1>(tup).value();
+  string s = std::get<1>(tup).value();
   // ...
-});
-
+
}); +

collect() #

collect() is similar to collectAll(), but will terminate early if an exception is raised by any of the input Futures. Therefore, the returned Future is of type std::vector<T>. Like collectAll(), input Futures are moved in and are no longer valid, and the resulting Future's vector will contain the results of each input Future in the same order they were passed in (if all are successful). For instance:

-
collect(fs).then([](const std::vector<T>& vals) {
-  for (const auto& val : vals) {
+
collect(fs).then([](const std::vector<T>& vals) {
+  for (const auto& val : vals) {
     // handle each response
   }
 })
@@ -563,10 +563,10 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac
 });
 
 // Or using a Try:
-collect(fs).then([](const Try<std::vector<T>>& t) {
+collect(fs).then([](const Try<std::vector<T>>& t) {
  // ...
 });
-
+

collect() variadic #

@@ -577,45 +577,45 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

collectN, like collectAll(), takes a collection of Futures, or a pair of iterators thereof, but it also takes a size_t N and will complete once N of the input futures are complete. It returns a Future<std::vector<std::pair<size_t, Try<T>>>>. Each pair holds the index of the corresponding Future in the original collection as well as its result, though the pairs themselves will be in arbitrary order. Like collectAll(), collectN() moves in the input Futures, so your copies are no longer valid. If multiple input futures complete "simultaneously" or are already completed, winners are chosen but the choice is undefined.

// Wait for 5 of the input futures to complete
-collectN(fs, 5,
+collectN(fs, 5,
   [](const std::vector<std::pair<size_t, Try<int>>>& tries){
     // there will be 5 pairs
-    for (const auto& pair : tries) {
+    for (const auto& pair : tries) {
       size_t index = pair.first;
-      int result = pair.second.value();
+      int result = pair.second.value();
       // ...
-    }
+    }
   });
-
+

collectAny() #

collectAny() also takes a collection of Futures (or a pair of iterators thereof), but it completes as soon as any of the input Futures completes. It returns a Future<std::pair<size_t, Try<T>>> which holds the index of the first completed Future along with its result. The input futures are moved in, so your copies are no longer valid. If multiple input futures complete "simultaneously" or are already completed, a winner is chosen but the choice is undefined. For example:

-
collectAny(fs, [](const std::pair<size_t, Try<int>>& p){
+
collectAny(fs, [](const std::pair<size_t, Try<int>>& p){
   size_t index = p.first;
-  int result = p.second.value();
+  int result = p.second.value();
   // ...
-});
-
+
}); +

map() #

map() is the Futures equivalent of the higher order function map. It takes a collection of Future<A> (or a pair of iterators thereof) and a function that can be passed to Future<A>::then(), and in turn calls then() with the function on each input Future. It returns a vector of the resultant Futures in the order they were passed in. This is simple sugar for:

-
std::vector<Future<A>> fs;
-std::vector<Future<B>> fs2;
-for (auto it = fs.begin(); it < fs.end(); it++) {
+
std::vector<Future<A>> fs;
+std::vector<Future<B>> fs2;
+for (auto it = fs.begin(); it < fs.end(); it++) {
   fs2.push_back(it->then(func));
-}
+}
 

For instance, say you have some expensive RPC that fetches an int and you'd like to do expensive processing on each of many calls to this RPC. collect() or collectAll() might not be wise since they wait for all the results to be ready, while you'd rather process the integers as they arrive. You could use map() in this scenario:

-
auto fs2 = map(fs, expensiveProcessingFunc);
+
auto fs2 = map(fs, expensiveProcessingFunc);
 // You probably now want to wait for all of these to complete. Call
 // collect() or collectAll() on fs2 to obtain such a Future.
-
+

reduce() #

@@ -625,36 +625,36 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

For instance, if you have a collection of Future<int> and you want a Future<bool> that contains true if and only if all the input ints are equal to zero, you might write:

-
reduce(fs, true, [](bool b, int i){
+
reduce(fs, true, [](bool b, int i){
   // You could also return a Future<bool> if you needed to
   return b && (i == 0); 
-})
+})
 .then([](bool result){
   // result is true if all inputs were zero
 });
 // You could use onError or Try here in case one of your input Futures
 // contained an exception or if your reducing function threw an exception 
-
+

To demonstrate the exception handling case, suppose you have a collection of Future<T> and you want a Future<bool> that contains true if all the input Futures are non-exceptional:

-
reduce(fs, true, [](bool b, Try<T> t){
+
reduce(fs, true, [](bool b, Try<T> t){
   return b && t.hasValue();
-})
+})
 .then([](bool result){
   // result is true if all inputs were non-exceptional
 });
-
+

And finally one example where we're not reducing to a bool - here's how you might calculate the sum of a collection of Future<int>:

-
reduce(fs, 0, [](int a, int b){
+
reduce(fs, 0, [](int a, int b){
   return a + b;
-})
+})
 .then([](int sum){
   // ...
 });
-
+

See the reduce() tests in the Future tests for a more complete catalog of possibilities.

@@ -684,15 +684,15 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

So what's the catch? Let's look at the following example of multithreaded Futures code:

// Thread A
-Promise<Unit> p;
-auto f = p.getFuture();
+Promise<Unit> p;
+auto f = p.getFuture();
 
 // Thread B
-f.then(x).then(y);
+f.then(x).then(y);
 
 // Thread A
-p.setValue();
-
+
p.setValue(); +

In which thread are x and y executed? Unfortunately, it depends. There is a race between setting the callbacks and fulfilling the promise. If setting the callbacks wins, they will be executed in thread A when the Promise is fulfilled. If setting the value wins, they will be executed in thread B as soon as they are set. If setValue() sneaks in at just the right time between the two then() calls, then x will be executed in thread A and y will be executed in thread B. You could imagine that this nondeterminism might become unwieldy or downright unacceptable. Thankfully, there's a mechanism to resolve this race and give you fine-grained control over your execution model.

@@ -700,18 +700,18 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

Futures have a method called via() which takes an Executor. Executor is a simple interface that requires only the existence of an add(std::function<void()> func) method which must be thread safe and must execute the provided function somehow, though not necessarily immediately. via() guarantees that a callback set on the Future will be executed on the given Executor. For instance:

-
makeFutureWith(x)
+
makeFutureWith(x)
   .via(exe1).then(y)
   .via(exe2).then(z);
-
+

In this example, y will be executed on exe1, and z will be executed on exe2. This is a fairly powerful abstraction. It not only solves the above race, but gives you clear, concise, and self-documenting control over your execution model. One common pattern is having different executors for different types of work (e.g. an IO-bound pool spinning on event bases doing your network IO and a CPU-bound thread pool for expensive work) and switching between them with via().

There is also a static function via() that creates a completed Future<Unit> that is already set up to call back on the provided Executor, and via(Executor&,Func) returns a Future for executing a function via an executor.

-
via(exe).then(a);
-via(exe, a).then(b);
-
+
via(exe).then(a);
+via(exe, a).then(b);
+

Or, pass an Executor to then() #

@@ -730,7 +730,7 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac
  • ManualExecutor only executes work when manually cranked. This is useful for testing.
  • InlineExecutor executes work immediately inline
  • QueuedImmediateExecutor is similar to InlineExecutor, but work added during callback execution will be queued instead of immediately executed
  • -
  • ScheduledExecutor is a subinterface of Executor that supports scheduled (i.e. delayed) execution. There aren't many implementations yet, see T5924392
  • +
  • ScheduledExecutor is a subinterface of Executor that supports scheduled (i.e. delayed) execution. There aren't many implementations yet, see T5924392
  • Thrift's ThreadManager is an Executor but we aim to deprecate it in favor of the aforementioned CPUThreadPoolExecutor
  • FutureExecutor wraps another Executor and provides Future<T> addFuture(F func) which returns a Future representing the result of func. This is equivalent to futures::async(executor, func) and the latter should probably be preferred.
  • Timeouts and related features

    Futures provide a number of timing-related features. Here's an overview.

    Timing implementation #

    @@ -787,7 +787,7 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

    get() and wait() with timeouts #

    -

    get() and wait(), which are detailed in the Testing article, optionally take timeouts:

    +

    get() and wait(), which are detailed in the Testing article, optionally take timeouts:

    Future<int> foo();
     // Will throw TimedOut if the Future doesn't complete within one second of
    @@ -853,24 +853,24 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac
     
     

    Let's say we want to test the following interface:

    -
    Future<bool> isPrime(int n);
    -
    +
    Future<bool> isPrime(int n);
    +

    We could make a couple of calls and set expectations on the resultant futures via value():

    -
    EXPECT_TRUE(isPrime(7).value());
    -EXPECT_FALSE(isPrime(8).value());
    -
    +
    EXPECT_TRUE(isPrime(7).value());
    +EXPECT_FALSE(isPrime(8).value());
    +

    But what if isPrime() is asynchronous (e.g. makes an async call to another service that computes primeness)? It's now likely that you'll call value() before the Future is complete, which will throw a FutureNotReady exception.

    A naive approach is to spin until the Future is complete:

    // Spin until ready. Gross. Don't do this.
    -auto f = isPrime(7);
    -while (!f.isReady()) {}
    +auto f = isPrime(7);
    +while (!f.isReady()) {}
     EXPECT_TRUE(f.value());
    -
    +

    Thankfully, we have some better options in the form of Future<T>::get() and Future<T>::wait().

    @@ -878,21 +878,21 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

    T Future<T>::get() blocks until the Future is complete and either returns a moved out copy of the value or throws any contained exception. You can use it like so.

    -
    EXPECT_TRUE(isPrime(7).get());
    -
    +
    EXPECT_TRUE(isPrime(7).get());
    +
    -

    Keep in mind that some other thread had better complete the Future, because the thread that calls get() will block. Also, get() optionally takes a timeout after which its throws a TimedOut exception. See the Timeouts article for more information.

    +

    Keep in mind that some other thread had better complete the Future, because the thread that calls get() will block. Also, get() optionally takes a timeout after which its throws a TimedOut exception. See the Timeouts article for more information.

    wait() #

    Future<T> Future<T>::wait() is similar to get() in that it blocks until the Future is complete. However, instead of returning a value or throwing an exception, it returns a new completed Future with the result of the original Future. One use case is when you'd rather not have the throwing behavior of get() so that you can check for exceptions separately without a try/catch. For example:

    -
    auto f = isPrime(7).wait();
    -EXPECT_FALSE(f.getTry().hasException());
    -EXPECT_TRUE(f.value());
    -
    +
    auto f = isPrime(7).wait();
    +EXPECT_FALSE(f.getTry().hasException());
    +EXPECT_TRUE(f.value());
    +
    -

    Like get(), wait() optionally takes a timeout. Again, see the Timeouts article.

    +

    Like get(), wait() optionally takes a timeout. Again, see the Timeouts article.

    getVia() and waitVia() #

    @@ -900,22 +900,22 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

    Given this:

    -
    auto f = doAsyncWorkOnEventBase(&eventBase);
    -
    +
    auto f = doAsyncWorkOnEventBase(&eventBase);
    +

    Don't do this:

    -
    while (!f.isReady()) {
    +
    while (!f.isReady()) {
       eb.loop();
    -}
    +}
     

    Do one of these instead:

    -
    auto val = f.getVia(&eventBase);
    +
    auto val = f.getVia(&eventBase);
     // or
    -f.waitVia(&eb).then([](Value val){ ... });
    -
    +
    f.waitVia(&eb).then([](Value val){ ... }); +

    Using gmock #

    @@ -924,49 +924,49 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

    The canonical approach to mocking interfaces that involve noncopyable objects is to override your interface with a dummy method that simply calls a mock method that has had the noncopyable components stripped or replaced. For Futures, this usually means returning/passing contained values directly and synchronously, which shouldn't be a problem since your mocks won't actually be asynchronous. Here is a very contrived but demonstrative example:

    // The async interface we want to mock
    -class AsyncClient {
    - public:
    +class AsyncClient {
    + public:
       virtual Future<int> foo(int i);
    -};
    +};
     
     // The mock implementation
    -class MockAsyncClient : public AsyncClient {
    - public:
    +class MockAsyncClient : public AsyncClient {
    + public:
       // Declare a mock method foo_ that takes an int and returns an int
       MOCK_METHOD1(foo_, int(int));
     
       // Plug the mock into an override of the interface
    -  Future<int> foo(int i) override {
    +  Future<int> foo(int i) override {
         // Lift the result back into a Future before returning
         return makeFuture<int>(foo_(i));
    -  }
    +  }
     };
     
     // Let's say that we're testing a class MyProxy that simply forwards foo()
     // calls to AsyncClient and returns the result
    -class MyProxy {
    - public:
    +class MyProxy {
    + public:
       Future<int> foo(int i) {
         return client->foo(i);
    -  }
    +  }
       void setClient(AsyncClient* client);
    - private:
    -  AsyncClient* client;
    -};
    + private:
    +  AsyncClient* client;
    +};
     
     // Now, in our testing code
    -MyProxy proxy;
    -MockAsyncClient mockClient;
    +MyProxy proxy;
    +MockAsyncClient mockClient;
     // Inject the mock
    -proxy.setClient(&mockClient)
    +proxy.setClient(&mockClient)
     // Set an expectation on the mock to be called with 42 and return 84
     EXPECT_CALL(mockClient, foo_(42)).WillOnce(Return(84));
     // Trigger the call
    -auto f = MyProxy.foo(42);
    +auto f = MyProxy.foo(42);
     // If everything has been mocked out synchronously, we can just check the
     // value of the future directly
    -EXPECT_EQ(84, f.value());
    -

    Pitfalls

    EventBase, EventBaseManager, Executor #

    +EXPECT_EQ(84, f.value()); +

    Pitfalls

    EventBase, EventBaseManager, Executor #

    It's not uncommon to hit a snag (especially when using via()) where you're hanging for (a) being on the wrong thread (b) talking to an EventBase which is not actually spinning (loopForever).

    @@ -981,49 +981,72 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

    The danger with lambdas is you'll try to read a variable that's gone

    -
    Object obj = ...;
    -return future1.then([&] {
    +
    Object obj = ...;
    +return future1.then([&] {
         // ..work..
         obj.method();
           // woops object is gone from the 
           // stack when this actually runs
    -});
    -
    +
    }); +

    Sometimes it makes sense to copy inputs. Sometimes that's too expensive and a shared_ptr is best. Sometimes the nature of things lends itself to the contract "this won't go away" and you take a raw pointer, but this should only be used when it's a very natural fit. In particular, you don't want people wishing you took a shared pointer and having to do something like this to work around it:

    -
    auto foo = make_shared<Foo>();
    -yourFunction(foo.get(),
    +
    auto foo = make_shared<Foo>();
    +yourFunction(foo.get(),
       [foo]{ 
          /* callback doesn't use foo, but captures the 
           * shared pointer to keep it alive 
           */
     });
    -
    +

    In general: prefer taking inputs by value if they're small enough if inputs are big (measurably expensive to copy), then keep them on the heap and prefer a shared_ptr if you are really sure you need to get more fancy, put on your wizard hat and go to it ;)

    -
    Object obj = ...;
    -return future1.then([obj] {  // capture by value
    +
    Object obj = ...;
    +return future1.then([obj] {  // capture by value
         // ..work..
         obj.method();
           // works on copy of obj
    -});
    -
    +
    }); +

    If Object is large:

    -
    auto optr = makeShared<Object>(...);
    -return future1.then([optr] {  // copy ptr, use count = 2
    +
    auto optr = makeShared<Object>(...);
    +return future1.then([optr] {  // copy ptr, use count = 2
         // ..work..
         optr->method();
           // works on original object
         // use-count for optr goes to 0 and object destructs
    -});
    -

    Future as a Monad

    A semi-formal and totally optional analysis of Future as a monad.

    Future is a monad. You don't need to know this or what it means to use Futures, but if you are curious, want to understand monads better, or eat functional flakes for breakfast, then keep reading this extremely extracurricular document.

    +}); +
    + +

    Using std::move with lambda capture #

    + +

    If you have move-only objects, like unique_ptr, then you can use generalized lambda capture (C++14) syntax:

    + +
    auto moveOnly = folly::make_unique<Object>(...);
    +return future1.then([lambdaObj = std::move(moveOnly)] {  
    +    // ...
    +});
    +
    + +

    Since you can only std::move() out of an object once, you can't have:

    + +
    auto moveOnly = folly::make_unique<Object>(...);
    +return future1.then([lambdaObj = std::move(moveOnly)] {  
    +    // Do work:
    +})
    +.onError([lambdaObj = std::move(moveOnly)] { 
    +    // Error handling:
    +});
    +
    + +

    And note, the construction order of the lambdas in GCC is somewhat counter-intuitive when you have several declared in one statement. The lambda instance for the .onError() case will be constructed first (the legal std::move), and then the '.then()' clause lambda. See https://godbolt.org/g/B51b77.

    Future as a Monad

    A semi-formal and totally optional analysis of Future as a monad.

    Future is a monad. You don't need to know this or what it means to use Futures, but if you are curious, want to understand monads better, or eat functional flakes for breakfast, then keep reading this extremely extracurricular document.

    Let's review the definition of a monad. Formal definitions are mathematical and/or in Haskellese and therefore opaque to imperative mortals. But here's a simplified description using a subset of Haskell type notation that is useful but not confusing:

    @@ -1049,39 +1072,39 @@ if you are really sure you need to get more fancy, put on your wizard hat and go

    I won't try to explain that, there are many blog posts and wiki pages that try to do that. Instead, I'll substitute the equivalent Future monad expressions, and the whole thing will (probably) start to make sense. First, a simplified Future type:

    -
    template <class A>
    +
    template <class A>
     struct Future {
       // The constructor that takes a value is "unit"
       Future(A);
     
       // "then" is "bind"
    -  template <class B>
    +  template <class B>
       Future<B> then(std::function<Future<B>(A));
     
    -  ...
    +  ...
     };
     
     // "makeFuture" is also "unit", and we will need it because constructors can't
     // really be converted to std::function (AFAIK)
    -template <class A>
    +template <class A>
     Future<A> makeFuture(A);
    -
    +

    Now, the three axioms (Futures don't define operator== but you get the idea):

    // Left Identity
    -A a;
    -Future<A>(a).then(f) == f(a)
    +A a;
    +Future<A>(a).then(f) == f(a)
     
     // Right Identity
     Future<A> m;
    -m.then(makeFuture<A>) == m
    +m.then(makeFuture<A>) == m
     
     // Associativity
     Future<A> m;
    -std::function<Future<B>(A)> f;
    -std::function<Future<C>(B)> g;
    -m.then(f).then(g) == m.then([](A x) { return f(x).then(g); })
    +std::function<Future<B>(A)> f;
    +std::function<Future<C>(B)> g;
    +m.then(f).then(g) == m.then([](A x) { return f(x).then(g); })
     

    So, in plain english this says a monad like Future has a way to get stuff in the monad (unit/makeFuture), and a way to chain things together (bind/then). unit semantics are unsurprising, and chaining is the same as nesting. Something that behaves this way is a monad, and Future is a monad.

    @@ -1110,7 +1133,7 @@ The three laws refer to a different formulation of the axioms, in terms of the K

    We accidentally implemented this operator, and called it chain. Then we removed it in favor of Future::thenMulti. But it totally existed, so use your imagination:

    // Left Identity
    -chain(makeFuture, g) ≡ g
    +chain(makeFuture, g) ≡ g
     // Right Identity
     chain(f, makeFuture) ≡ f
     // Associativity
    -- 
    2.34.1