get()
<experimental/future>
stlab::future<>
which has additional capabilities and minor differences in syntaxfuture
, using .then()
.then()
returns a new future that can be used for more continuations{
auto p = async(default_executor, []{ return 42; });
auto q = p.then([](int x){ cout << x << endl; });
auto r = q.then([]{ cout << "done!" << endl; });
blocking_get(r); // <-- DON'T DO THIS IN REAL CODE!!!
}
namespace bcc {
struct shared_pool {
unordered_set<string> _pool;
sequential_process _process;
auto insert(string) -> stlab::future<const string*>;
};
auto shared_pool::insert(string a) -> stlab::future<const string*> {
return async_packaged(_process, [this, _a = move(a)]() mutable {
return &*_pool.insert(move(_a)).first;
});
}
}
class interned_string {
// struct shared_pool
static auto pool() -> shared_pool& {
static shared_pool result;
return result;
}
shared_future<const std::string*> _string;
public:
interned_string(string a) : _string(pool().insert(move(a))) {}
auto str() const {
return *_string.get(); // <---- BLOCKING!!!
}
};
namespace {
class interned_string {
// struct shared_pool
static auto pool() -> shared_pool& {
static shared_pool result;
return result;
}
stlab::future<const string*> _string; // or std::experimental::shared_future
public:
interned_string(string a) : _string(pool().insert(move(a))) {}
auto str() const -> stlab::future<reference_wrapper<const string>> {
return _string.then([](const string* p) { return cref(*p); });
}
};
} // namespace
{
interned_string s("Hello World!"s);
auto done = s.str().then([](const string& s){
cout << s << '\n';
});
blocking_get(done);
}
future
(or shared_future
)when_all()
{
auto p = async(default_executor, []{ return 42; });
auto q = async(default_executor, []{ return 5; });
auto done = when_all(default_executor, [](int x, int y){ cout << x + y << endl; }, p, q);
blocking_get(done);
}