docs/classtf_1_1Future.html
class to access the result of an execution
tf::Future is a derived class from std::future that will eventually hold the execution result of a submitted taskflow (tf::Executor::run series). In addition to the base methods inherited from std::future, you can call tf::Future::cancel to cancel the execution of the running taskflow associated with this future object. The following example cancels a submission of a taskflow that contains 1000 tasks each running one second.
tf::Executor executor;tf::Taskflow taskflow;for(int i=0; i\<1000; i++) {taskflow.emplace([](){std::this\_thread::sleep\_for(std::chrono::seconds(1));});}// submit the taskflowtf::Future fu = executor.run(taskflow);// request to cancel the submitted execution abovefu.cancel();// wait until the cancellation finishesfu.get();
Future() defaulteddefault constructorFuture(const Future&) deleteddisabled copy constructorFuture(Future&&) defaulteddefault move constructorFuture(std::future<T>&&)constructs *this from a std::future
auto operator=(const Future&) -> Future& deleteddisabled copy assignmentauto operator=(Future&&) -> Future& defaulteddefault move assignmentauto cancel() -> boolcancels the execution of the running taskflow associated with this future object
cancels the execution of the running taskflow associated with this future object
| Returns | true if the execution can be cancelled or false if the execution has already completed |
When you request a cancellation, the executor will stop scheduling any tasks onwards. Tasks that are already running will continue to finish as their executions are non-preemptive. You can call tf::Future::wait to wait for the cancellation to complete.
// create a taskflow of four tasks and submit it to an executortaskflow.emplace([](){ std::cout \<\< "Task A\n"; },[](){ std::cout \<\< "Task B\n"; },[](){ std::cout \<\< "Task C\n"; },[](){ std::cout \<\< "Task D\n"; });auto future = executor.run(taskflow);// cancel the execution of the taskflow and wait until it finishes all running tasksfuture.cancel();future.wait();
In the above example, we submit a taskflow of four tasks to the executor and then issue a cancellation to stop its execution. Since the cancellation is non-deterministic with the executor runtime, we may still see some tasks complete their executions or none.