Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::cancellable_future< R > Class Template Reference

A future wrapper that supports cancellation. More...

#include <cancellable_future.h>

Collaboration diagram for kcenon::thread::cancellable_future< R >:
Collaboration graph

Public Types

using value_type = R
 

Public Member Functions

 cancellable_future (std::future< R > future, cancellation_token token)
 Construct a cancellable_future.
 
 ~cancellable_future ()=default
 
 cancellable_future (const cancellable_future &)=delete
 
cancellable_futureoperator= (const cancellable_future &)=delete
 
 cancellable_future (cancellable_future &&) noexcept=default
 
cancellable_futureoperator= (cancellable_future &&) noexcept=default
 
auto get () -> common::Result< R >
 Wait for and retrieve the result.
 
auto get_for (std::chrono::milliseconds timeout) -> common::Result< std::optional< R > >
 Wait for the result with timeout.
 
auto is_ready () const -> bool
 Check if the result is ready.
 
auto is_cancelled () const -> bool
 Check if the operation was cancelled.
 
void cancel ()
 Request cancellation of the operation.
 
auto valid () const -> bool
 Check if the future is valid.
 
void wait () const
 Wait for the result to become ready.
 
template<typename Rep , typename Period >
auto wait_for (const std::chrono::duration< Rep, Period > &timeout) const -> std::future_status
 Wait for the result with timeout.
 
auto get_token () const -> cancellation_token
 Get the cancellation token.
 

Private Attributes

std::future< R > future_
 
cancellation_token token_
 

Detailed Description

template<typename R>
class kcenon::thread::cancellable_future< R >

A future wrapper that supports cancellation.

Template Parameters
RThe result type of the future

This class wraps a std::future and a cancellation_token together, providing unified access to both the result and cancellation status.

Thread Safety

  • All methods are thread-safe.
  • The cancellation token can be shared across multiple contexts.

Usage Example

auto [future, token] = pool->submit_cancellable([] {
// Long-running work
return compute_result();
});
// Later, if we need to cancel:
future.cancel();
// Or wait with timeout:
if (auto result = future.get_for(std::chrono::seconds(5))) {
process(*result);
} else {
handle_timeout();
}
A template class representing either a value or an error.

Definition at line 56 of file cancellable_future.h.

Member Typedef Documentation

◆ value_type

template<typename R >
using kcenon::thread::cancellable_future< R >::value_type = R

Definition at line 58 of file cancellable_future.h.

Constructor & Destructor Documentation

◆ cancellable_future() [1/3]

template<typename R >
kcenon::thread::cancellable_future< R >::cancellable_future ( std::future< R > future,
cancellation_token token )
inline

Construct a cancellable_future.

Parameters
futureThe underlying std::future
tokenThe cancellation token for this operation

Definition at line 66 of file cancellable_future.h.

67 : future_(std::move(future))
68 , token_(std::move(token))
69 {}

◆ ~cancellable_future()

template<typename R >
kcenon::thread::cancellable_future< R >::~cancellable_future ( )
default

◆ cancellable_future() [2/3]

template<typename R >
kcenon::thread::cancellable_future< R >::cancellable_future ( const cancellable_future< R > & )
delete

◆ cancellable_future() [3/3]

template<typename R >
kcenon::thread::cancellable_future< R >::cancellable_future ( cancellable_future< R > && )
defaultnoexcept

Member Function Documentation

◆ cancel()

template<typename R >
void kcenon::thread::cancellable_future< R >::cancel ( )
inline

Request cancellation of the operation.

This signals the associated job to stop. The job must cooperatively check its cancellation token for this to have effect.

Definition at line 162 of file cancellable_future.h.

162 {
163 token_.cancel();
164 }
void cancel()
Cancels the operation.

References kcenon::thread::cancellation_token::cancel(), and kcenon::thread::cancellable_future< R >::token_.

Here is the call graph for this function:

◆ get()

template<typename R >
auto kcenon::thread::cancellable_future< R >::get ( ) -> common::Result<R>
inlinenodiscard

Wait for and retrieve the result.

Returns
common::Result<R> containing the value on success, or an error if the operation was cancelled or threw an exception

This method blocks until the result is ready or the operation is cancelled.

Definition at line 91 of file cancellable_future.h.

91 {
92 if (token_.is_cancelled()) {
94 }
95 try {
96 return common::Result<R>::ok(future_.get());
97 } catch (const std::exception& e) {
99 } catch (...) {
100 return make_error_result<R>(error_code::unknown_error, "Unknown exception in future");
101 }
102 }
bool is_cancelled() const
Checks if the token has been canceled.
common::VoidResult make_error_result(error_code code, const std::string &message="")
Create a common::VoidResult error from a thread::error_code.

References kcenon::thread::cancellation_token::is_cancelled(), kcenon::thread::job_execution_failed, kcenon::thread::make_error_result(), kcenon::thread::cancellable_future< R >::token_, and kcenon::thread::unknown_error.

Here is the call graph for this function:

◆ get_for()

template<typename R >
auto kcenon::thread::cancellable_future< R >::get_for ( std::chrono::milliseconds timeout) -> common::Result<std::optional<R>>
inlinenodiscard

Wait for the result with timeout.

Parameters
timeoutMaximum time to wait
Returns
common::Result<std::optional<R>> containing the value if ready, std::nullopt on timeout, or an error if cancelled/failed

This method waits up to the specified timeout for the result. Returns std::nullopt in the Result if the timeout expires.

Definition at line 114 of file cancellable_future.h.

116 {
117 if (token_.is_cancelled()) {
119 }
120
121 auto status = future_.wait_for(timeout);
122 if (status == std::future_status::ready) {
123 try {
124 return common::Result<std::optional<R>>::ok(future_.get());
125 } catch (const std::exception& e) {
128 } catch (...) {
130 error_code::unknown_error, "Unknown exception in future");
131 }
132 }
133 return common::Result<std::optional<R>>::ok(std::nullopt);
134 }

References kcenon::thread::cancellable_future< R >::future_, kcenon::thread::cancellation_token::is_cancelled(), kcenon::thread::job_execution_failed, kcenon::thread::make_error_result(), kcenon::thread::operation_canceled, kcenon::thread::cancellable_future< R >::token_, and kcenon::thread::unknown_error.

Here is the call graph for this function:

◆ get_token()

template<typename R >
auto kcenon::thread::cancellable_future< R >::get_token ( ) const -> cancellation_token
inlinenodiscard

Get the cancellation token.

Returns
A copy of the cancellation token

Definition at line 202 of file cancellable_future.h.

202 {
203 return token_;
204 }

References kcenon::thread::cancellable_future< R >::token_.

◆ is_cancelled()

template<typename R >
auto kcenon::thread::cancellable_future< R >::is_cancelled ( ) const -> bool
inlinenodiscard

Check if the operation was cancelled.

Returns
true if cancellation was requested, false otherwise

Definition at line 151 of file cancellable_future.h.

151 {
152 return token_.is_cancelled();
153 }

References kcenon::thread::cancellation_token::is_cancelled(), and kcenon::thread::cancellable_future< R >::token_.

Here is the call graph for this function:

◆ is_ready()

template<typename R >
auto kcenon::thread::cancellable_future< R >::is_ready ( ) const -> bool
inlinenodiscard

Check if the result is ready.

Returns
true if the result is available, false otherwise

Definition at line 141 of file cancellable_future.h.

141 {
142 return future_.wait_for(std::chrono::seconds(0)) ==
143 std::future_status::ready;
144 }

References kcenon::thread::cancellable_future< R >::future_.

◆ operator=() [1/2]

template<typename R >
cancellable_future & kcenon::thread::cancellable_future< R >::operator= ( cancellable_future< R > && )
defaultnoexcept

◆ operator=() [2/2]

template<typename R >
cancellable_future & kcenon::thread::cancellable_future< R >::operator= ( const cancellable_future< R > & )
delete

◆ valid()

template<typename R >
auto kcenon::thread::cancellable_future< R >::valid ( ) const -> bool
inlinenodiscard

Check if the future is valid.

Returns
true if the future has a shared state, false otherwise

Definition at line 171 of file cancellable_future.h.

171 {
172 return future_.valid();
173 }

References kcenon::thread::cancellable_future< R >::future_.

◆ wait()

template<typename R >
void kcenon::thread::cancellable_future< R >::wait ( ) const
inline

Wait for the result to become ready.

Blocks until the result is available.

Definition at line 180 of file cancellable_future.h.

180 {
181 future_.wait();
182 }

References kcenon::thread::cancellable_future< R >::future_.

◆ wait_for()

template<typename R >
template<typename Rep , typename Period >
auto kcenon::thread::cancellable_future< R >::wait_for ( const std::chrono::duration< Rep, Period > & timeout) const -> std::future_status
inlinenodiscard

Wait for the result with timeout.

Parameters
timeoutMaximum time to wait
Returns
The future status after waiting

Definition at line 191 of file cancellable_future.h.

193 {
194 return future_.wait_for(timeout);
195 }

References kcenon::thread::cancellable_future< R >::future_.

Member Data Documentation

◆ future_

◆ token_


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