ThreadedFuture Class Template
A future backed by an explicitly-owned std::jthread, for Behaviors that offload work to a background thread and poll for completion (e.g. in onRunning()). More...
Declaration
class moveit_pro::behaviors::ThreadedFuture<T> { ... }
Included Headers
Public Constructors Index
template <typename T> | |
| ThreadedFuture ()=default | |
|
Constructs an empty, invalid future. Matches default-constructed std::future<T>. More... | |
template <typename T> | |
| ThreadedFuture (ThreadedFuture &&) noexcept=default | |
template <typename T> | |
| ThreadedFuture (const ThreadedFuture &)=delete | |
template < ... > | |
| ThreadedFuture (F &&task) | |
|
Runs task on a newly-created thread and captures its result. More... | |
Public Operators Index
template <typename T> | |
| ThreadedFuture & | operator= (ThreadedFuture &&) noexcept=default |
template <typename T> | |
| ThreadedFuture & | operator= (const ThreadedFuture &)=delete |
Public Member Functions Index
template <typename T> | |
| bool | valid () const noexcept |
template <typename Rep, typename Period> | |
| std::future_status | wait_for (const std::chrono::duration< Rep, Period > &timeout_duration) const |
template <typename T> | |
| void | wait () const |
template <typename T> | |
| T | get () |
Private Member Attributes Index
template <typename T> | |
| std::jthread | thread_ |
template <typename T> | |
| std::future< T > | future_ |
Description
A future backed by an explicitly-owned std::jthread, for Behaviors that offload work to a background thread and poll for completion (e.g. in onRunning()).
Deliberately mirrors the subset of std::future's interface (valid(), wait_for(), wait(), get()) that this pattern uses, so it is a drop-in replacement for the std::future<T> member that std::async returns.
Unlike std::async(std::launch::async, ...), whose thread is owned by an implementation-defined pool, the background thread here is a std::jthread owned directly by this object. Like the future returned by std::async, destroying (or reassigning) a ThreadedFuture while its task is still running blocks until the task finishes: the owned std::jthread joins in its own destructor and move-assignment.
That guarantee is scoped to this object only. When a ThreadedFuture is a class member and its task captures this (or references to sibling members), members declared after the ThreadedFuture are destroyed before it joins — declare the ThreadedFuture as the last member so the join runs before anything the task dereferences is torn down.
- Template Parameters
-
T The type returned by the task.
Definition at line 37 of file threaded_future.hpp.
Public Constructors
ThreadedFuture()
| default |
Constructs an empty, invalid future. Matches default-constructed std::future<T>.
Definition at line 41 of file threaded_future.hpp.
ThreadedFuture()
| noexcept default |
Definition at line 43 of file threaded_future.hpp.
ThreadedFuture()
| delete |
Definition at line 48 of file threaded_future.hpp.
ThreadedFuture()
| inline explicit |
Runs task on a newly-created thread and captures its result.
Definition at line 53 of file threaded_future.hpp.
Public Operators
operator=()
| noexcept default |
Definition at line 44 of file threaded_future.hpp.
operator=()
| delete |
Definition at line 49 of file threaded_future.hpp.
Public Member Functions
get()
| inline |
Definition at line 76 of file threaded_future.hpp.
valid()
| inline noexcept |
Definition at line 60 of file threaded_future.hpp.
wait()
| inline |
Definition at line 71 of file threaded_future.hpp.
wait_for()
| inline |
Definition at line 66 of file threaded_future.hpp.
Private Member Attributes
future_
|
Definition at line 87 of file threaded_future.hpp.
thread_
|
Definition at line 86 of file threaded_future.hpp.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.9.8.