Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::cancellation_reason Struct Reference

Holds information about why a cancellation occurred. More...

#include <cancellation_reason.h>

Collaboration diagram for kcenon::thread::cancellation_reason:
Collaboration graph

Public Types

enum class  type {
  none , user_requested , timeout , deadline ,
  parent_cancelled , pool_shutdown , error
}
 The type of cancellation that occurred. More...
 

Public Member Functions

auto to_string () const -> std::string
 Converts the cancellation reason to a human-readable string.
 

Static Public Member Functions

static auto type_to_string (type t) -> std::string
 Converts the reason type enum to a string.
 

Public Attributes

type reason_type = type::none
 The type of cancellation that occurred.
 
std::string message
 Human-readable message describing the cancellation.
 
std::chrono::steady_clock::time_point cancel_time
 Time point when the cancellation occurred.
 
std::optional< std::exception_ptr > exception
 Optional exception that triggered the cancellation.
 

Detailed Description

Holds information about why a cancellation occurred.

When a cancellation token is cancelled, the reason provides details about what triggered the cancellation, when it occurred, and optionally any exception that caused it.

Usage Example

std::chrono::seconds{30});
// ... later, when cancelled ...
if (token.is_cancelled()) {
auto reason = token.get_reason();
if (reason) {
std::cout << "Cancelled: " << reason->to_string() << std::endl;
}
}
static auto create_with_timeout(std::chrono::milliseconds timeout) -> enhanced_cancellation_token
Creates a token that auto-cancels after the specified timeout.

Definition at line 50 of file cancellation_reason.h.

Member Enumeration Documentation

◆ type

The type of cancellation that occurred.

Enumerator
none 

No cancellation (default state)

user_requested 

Explicit cancel() call by user.

timeout 

Timeout duration expired.

deadline 

Deadline time point reached.

parent_cancelled 

Parent token was cancelled.

pool_shutdown 

Thread pool is shutting down.

error 

Cancellation triggered by an error.

Definition at line 56 of file cancellation_reason.h.

57 {
58 none,
60 timeout,
61 deadline,
64 error
65 };
@ pool_shutdown
Thread pool is shutting down.
@ parent_cancelled
Parent token was cancelled.
@ deadline
Deadline time point reached.
@ none
No cancellation (default state)
@ user_requested
Explicit cancel() call by user.
@ timeout
Timeout duration expired.
@ error
Cancellation triggered by an error.

Member Function Documentation

◆ to_string()

auto kcenon::thread::cancellation_reason::to_string ( ) const -> std::string
inlinenodiscard

Converts the cancellation reason to a human-readable string.

Returns
A string describing the cancellation reason.

Definition at line 83 of file cancellation_reason.h.

84 {
85 std::string type_str;
86 switch (reason_type)
87 {
88 case type::none:
89 type_str = "none";
90 break;
92 type_str = "user_requested";
93 break;
94 case type::timeout:
95 type_str = "timeout";
96 break;
97 case type::deadline:
98 type_str = "deadline";
99 break;
101 type_str = "parent_cancelled";
102 break;
104 type_str = "pool_shutdown";
105 break;
106 case type::error:
107 type_str = "error";
108 break;
109 }
110
111 std::string result = "cancellation_reason{type=" + type_str;
112 if (!message.empty())
113 {
114 result += ", message=\"" + message + "\"";
115 }
116 result += "}";
117 return result;
118 }
type reason_type
The type of cancellation that occurred.
std::string message
Human-readable message describing the cancellation.

References deadline, error, message, none, parent_cancelled, pool_shutdown, reason_type, timeout, and user_requested.

◆ type_to_string()

static auto kcenon::thread::cancellation_reason::type_to_string ( type t) -> std::string
inlinestaticnodiscard

Converts the reason type enum to a string.

Parameters
tThe reason type to convert.
Returns
A string representation of the reason type.

Definition at line 125 of file cancellation_reason.h.

126 {
127 switch (t)
128 {
129 case type::none:
130 return "none";
132 return "user_requested";
133 case type::timeout:
134 return "timeout";
135 case type::deadline:
136 return "deadline";
138 return "parent_cancelled";
140 return "pool_shutdown";
141 case type::error:
142 return "error";
143 }
144 return "unknown";
145 }

References deadline, error, none, parent_cancelled, pool_shutdown, timeout, and user_requested.

Member Data Documentation

◆ cancel_time

std::chrono::steady_clock::time_point kcenon::thread::cancellation_reason::cancel_time

◆ exception

std::optional<std::exception_ptr> kcenon::thread::cancellation_reason::exception

Optional exception that triggered the cancellation.

Definition at line 77 of file cancellation_reason.h.

Referenced by kcenon::thread::enhanced_cancellation_token::do_cancel().

◆ message

◆ reason_type


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