Future(Future&& that) : state_(std::move(that.state_)) {}
Future& operator=(Future&& that)
{
- state_ = std::move(that.stat_);
+ state_ = std::move(that.state_);
+ return *this;
}
/** Whether the future is valid:.
});
return result.get();
}
- // TODO, wait()
+ bool is_ready() const
+ {
+ if (!valid())
+ throw std::future_error(std::future_errc::no_state);
+ return future_.is_ready();
+ }
+ void wait()
+ {
+ if (!valid())
+ throw std::future_error(std::future_errc::no_state);
+ std::exception_ptr exception;
+ smx_process_t self = SIMIX_process_self();
+ simcall_run_blocking([this, &exception, self]{
+ try {
+ // When the kernel future is ready...
+ this->future_.then([this, self](simgrid::kernel::Future<T> value) {
+ // ...store it the simix kernel and wake up.
+ this->future_ = std::move(value);
+ simgrid::simix::unblock(self);
+ });
+ }
+ catch (...) {
+ exception = std::current_exception();
+ simgrid::simix::unblock(self);
+ }
+ });
+ }
// TODO, wait_for()
// TODO, wait_until()
private:
res = future.get();
XBT_INFO("kernelAsync with value returned with %i", res);
+ // Synchronize on a successul Future<int> and get the value:
+ future = simgrid::simix::kernelAsync([&] {
+ return kernel_defer(60, [] {
+ XBT_INFO("kernelAsync with value");
+ return 43;
+ });
+ });
+ XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
+ future.wait();
+ XBT_INFO("The future is %s", future.is_ready() ? "ready" : "not ready");
+ res = future.get();
+ XBT_INFO("kernelAsync with value returned with %i", res);
+
return 0;
}
> [Tremblay:master:(0) 30.000000] [test/INFO] kernelSync with value returned with 42
> [50.000000] [test/INFO] kernelAsync with value
> [Tremblay:master:(0) 50.000000] [test/INFO] kernelAsync with value returned with 43
+> [Tremblay:master:(0) 50.000000] [test/INFO] The future is not ready
+> [60.000000] [test/INFO] kernelAsync with value
+> [Tremblay:master:(0) 60.000000] [test/INFO] The future is ready
+> [Tremblay:master:(0) 60.000000] [test/INFO] kernelAsync with value returned with 43
\ No newline at end of file