Skip to main content

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

template <typename T>
class moveit_pro::behaviors::ThreadedFuture<T> { ... }

Included Headers

#include <threaded_future.hpp>

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>
boolvalid () const noexcept
template <typename Rep, typename Period>
std::future_statuswait_for (const std::chrono::duration< Rep, Period > &timeout_duration) const
template <typename T>
voidwait () const
template <typename T>
Tget ()

Private Member Attributes Index

template <typename T>
std::jthreadthread_
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.

warning

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()

template <typename T>
moveit_pro::behaviors::ThreadedFuture< T >::ThreadedFuture ()
default

Constructs an empty, invalid future. Matches default-constructed std::future<T>.

Definition at line 41 of file threaded_future.hpp.

ThreadedFuture()

template <typename T>
moveit_pro::behaviors::ThreadedFuture< T >::ThreadedFuture (ThreadedFuture &&)
noexcept default

Definition at line 43 of file threaded_future.hpp.

ThreadedFuture()

template <typename T>
moveit_pro::behaviors::ThreadedFuture< T >::ThreadedFuture (const ThreadedFuture &)
delete

Definition at line 48 of file threaded_future.hpp.

ThreadedFuture()

template <typename F, typename = std::enable_if_t<!std::is_same_v<std::remove_cvref_t<F>, ThreadedFuture>>>
moveit_pro::behaviors::ThreadedFuture< T >::ThreadedFuture (F && task)
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=()

template <typename T>
ThreadedFuture & moveit_pro::behaviors::ThreadedFuture< T >::operator= (ThreadedFuture &&)
noexcept default

Definition at line 44 of file threaded_future.hpp.

operator=()

template <typename T>
ThreadedFuture & moveit_pro::behaviors::ThreadedFuture< T >::operator= (const ThreadedFuture &)
delete

Definition at line 49 of file threaded_future.hpp.

Public Member Functions

get()

template <typename T>
T moveit_pro::behaviors::ThreadedFuture< T >::get ()
inline

Definition at line 76 of file threaded_future.hpp.

valid()

template <typename T>
bool moveit_pro::behaviors::ThreadedFuture< T >::valid ()
inline noexcept

Definition at line 60 of file threaded_future.hpp.

wait()

template <typename T>
void moveit_pro::behaviors::ThreadedFuture< T >::wait ()
inline

Definition at line 71 of file threaded_future.hpp.

wait_for()

template <typename Rep, typename Period>
std::future_status moveit_pro::behaviors::ThreadedFuture< T >::wait_for (const std::chrono::duration< Rep, Period > & timeout_duration)
inline

Definition at line 66 of file threaded_future.hpp.

Private Member Attributes

future_

template <typename T>
std::future<T> moveit_pro::behaviors::ThreadedFuture< T >::future_

Definition at line 87 of file threaded_future.hpp.

thread_

template <typename T>
std::jthread moveit_pro::behaviors::ThreadedFuture< T >::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.