MoveIt Pro API
Core Behaviors for MoveIt Pro
Loading...
Searching...
No Matches
moveit_pro::behaviors::ThreadedFuture< T > Class Template Reference

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...

#include <threaded_future.hpp>

Public Member Functions

 ThreadedFuture ()=default
 Constructs an empty, invalid future. Matches default-constructed std::future<T>.
 
 ThreadedFuture (ThreadedFuture &&) noexcept=default
 
ThreadedFutureoperator= (ThreadedFuture &&) noexcept=default
 
 ThreadedFuture (const ThreadedFuture &)=delete
 
ThreadedFutureoperator= (const ThreadedFuture &)=delete
 
template<typename F , typename = std::enable_if_t<!std::is_same_v<std::remove_cvref_t<F>, ThreadedFuture>>>
 ThreadedFuture (F &&task)
 Runs task on a newly-created thread and captures its result.
 
bool valid () const noexcept
 
template<typename Rep , typename Period >
std::future_status wait_for (const std::chrono::duration< Rep, Period > &timeout_duration) const
 
void wait () const
 
T get ()
 

Detailed Description

template<typename T>
class moveit_pro::behaviors::ThreadedFuture< T >

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
TThe type returned by the task.

Constructor & Destructor Documentation

◆ ThreadedFuture() [1/4]

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

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

◆ ThreadedFuture() [2/4]

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

◆ ThreadedFuture() [3/4]

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

◆ ThreadedFuture() [4/4]

template<typename T >
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)
inlineexplicit

Runs task on a newly-created thread and captures its result.

Member Function Documentation

◆ get()

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

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ valid()

template<typename T >
bool moveit_pro::behaviors::ThreadedFuture< T >::valid ( ) const
inlinenoexcept

◆ wait()

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

◆ wait_for()

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

The documentation for this class was generated from the following file: