Skip to content

Instantly share code, notes, and snippets.

@oberstet
Created March 26, 2014 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oberstet/9785686 to your computer and use it in GitHub Desktop.
Save oberstet/9785686 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#define BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
#include <boost/thread/future.hpp>
using namespace boost;
#if 1
int main() {
future<int> f1 = async([]() { return 1; });
future<int> f2 = async([]() { return 2; });
future<int> f3 = async([]() { return 3; });
f1.then([](decltype(f1) res) {
std::cout << "f1 done" << std::endl;
});
f2.then([](decltype(f2) res) {
std::cout << "f2 done" << std::endl;
});
f3.then([](decltype(f3) res) {
std::cout << "f3 done" << std::endl;
});
auto f12 = when_all(std::move(f1), std::move(f2));
auto f12d = f12.then([](decltype(f12)) {
std::cout << "f12 done" << std::endl;
});
auto f23 = when_all(std::move(f2), std::move(f3));
auto f23d = f23.then([](decltype(f23)) {
std::cout << "f23 done" << std::endl;
});
auto f123 = when_all(std::move(f12d), std::move(f23d));
auto fall = f123.then([](decltype(f123)) {
std::cout << "all done" << std::endl;
});
fall.get();
}
#else
int main() {
std::vector<future<int>> futures = {
async([]() { return 1; }),
async([]() { return 2; })
};
future<std::vector<future<int>>> f3 = when_all(std::begin(futures), std::end(futures));
auto f4 = f3.then([](decltype(f3)) {
std::cout << "done" << std::endl;
});
f4.get();
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment