|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Represents a unit of work (task) to be executed, typically by a job queue. More...
#include <job.h>


Public Member Functions | |
| auto | get_job_id () const -> std::uint64_t |
| Gets the unique ID of this job. | |
| auto | get_enqueue_time () const -> std::chrono::steady_clock::time_point |
| Gets the time when this job was created (enqueued). | |
| job (const std::string &name="job") | |
Constructs a new job with an optional human-readable name. | |
| job (const std::vector< uint8_t > &data, const std::string &name="data_job") | |
Constructs a new job with associated raw byte data and a name. | |
| virtual | ~job (void) |
Virtual destructor for the job class to allow proper cleanup in derived classes. | |
| auto | get_name (void) const -> std::string |
| Retrieves the name of this job. | |
| virtual auto | do_work (void) -> common::VoidResult |
| The core task execution method to be overridden by derived classes. | |
| virtual auto | set_cancellation_token (const cancellation_token &token) -> void |
| Sets a cancellation token that can be used to cancel the job. | |
| virtual auto | get_cancellation_token () const -> cancellation_token |
| Gets the cancellation token associated with this job. | |
| virtual auto | set_job_queue (const std::shared_ptr< job_queue > &job_queue) -> void |
Associates this job with a specific job_queue. | |
| virtual auto | get_job_queue (void) const -> std::shared_ptr< job_queue > |
Retrieves the job_queue associated with this job, if any. | |
| virtual auto | to_string (void) const -> std::string |
| Provides a string representation of the job for logging or debugging. | |
| auto | with_on_complete (std::function< void(common::VoidResult)> callback) -> job & |
| Attaches a completion callback to this job. | |
| auto | with_on_error (std::function< void(const common::error_info &)> callback) -> job & |
| Attaches an error callback to this job. | |
| auto | with_priority (job_priority priority) -> job & |
| Sets the priority level for this job. | |
| auto | with_cancellation (const cancellation_token &token) -> job & |
| Attaches a cancellation token to this job via composition. | |
| auto | with_retry (const retry_policy &policy) -> job & |
| Attaches a retry policy to this job. | |
| auto | with_timeout (std::chrono::milliseconds timeout) -> job & |
| Sets a timeout for job execution. | |
| auto | get_priority () const -> job_priority |
| Gets the priority level of this job. | |
| auto | get_retry_policy () const -> std::optional< retry_policy > |
| Gets the retry policy of this job. | |
| auto | get_timeout () const -> std::optional< std::chrono::milliseconds > |
| Gets the timeout duration for this job. | |
| auto | has_explicit_cancellation () const -> bool |
| Checks if this job has an explicit cancellation set via composition. | |
| auto | has_components () const -> bool |
| Checks if this job has any composed components. | |
Protected Member Functions | |
| auto | invoke_callbacks (const common::VoidResult &result) -> void |
| Invokes the completion callbacks if they are set. | |
Protected Attributes | |
| std::string | name_ |
| The descriptive name of the job, used primarily for identification and logging. | |
| std::vector< uint8_t > | data_ |
| An optional container of raw byte data that may be used by the job. | |
| std::weak_ptr< job_queue > | job_queue_ |
A weak reference to the job_queue that currently manages this job. | |
| cancellation_token | cancellation_token_ |
| The cancellation token associated with this job. | |
Private Member Functions | |
| auto | ensure_components () -> job_components & |
| Ensures components_ is allocated (lazy initialization). | |
Private Attributes | |
| std::uint64_t | job_id_ |
| Unique identifier for this job. | |
| std::chrono::steady_clock::time_point | enqueue_time_ |
| Time when the job was created. | |
| std::unique_ptr< job_components > | components_ |
| Composed components for this job. | |
Static Private Attributes | |
| static std::atomic< std::uint64_t > | next_job_id_ {0} |
| Static counter for generating unique job IDs. | |
Represents a unit of work (task) to be executed, typically by a job queue.
The job class provides a base interface for scheduling and executing discrete tasks within a multi-threaded environment. Derived classes can override the do_work() method to implement custom logic for their specific tasks.
do_work() will generally be called from a worker thread. If your task accesses shared data or interacts with shared resources, you must ensure your implementation is thread-safe.std::shared_ptr, making it easier to manage lifetimes across multiple threads.common::VoidResult from do_work().common::ok() indicates success.common::error_info indicates failure with a typed error code and message.| kcenon::thread::job::job | ( | const std::string & | name = "job" | ) |
Constructs a new job with an optional human-readable name.
Constructs a basic job with name only.
Use this constructor when your derived job class does not need to store any initial data beyond what you might manually store in derived class members.
| name | A descriptive name for the job (default is "job"). |
Implementation details:
Use Cases:
| name | Descriptive name for debugging and logging |
Definition at line 53 of file job.cpp.
| kcenon::thread::job::job | ( | const std::vector< uint8_t > & | data, |
| const std::string & | name = "data_job" ) |
Constructs a new job with associated raw byte data and a name.
Constructs a data-processing job with binary data.
This constructor is particularly useful if your job needs an inline payload or associated data that should be passed directly to do_work().
| data | A vector of bytes serving as the job's payload. |
| name | A descriptive name for the job (default is "data_job"). |
Implementation details:
Data Handling:
Performance Considerations:
| data | Binary data to be processed by this job |
| name | Descriptive name for debugging and logging |
|
virtual |
Virtual destructor for the job class to allow proper cleanup in derived classes.
Destroys the job and cleans up resources.
Implementation details:
Definition at line 100 of file job.cpp.
|
nodiscardvirtual |
The core task execution method to be overridden by derived classes.
Default implementation of work execution (must be overridden).
common::VoidResult indicating success or error:The base class implementation simply returns a success result. Override this method in a derived class to perform meaningful work.
job_queue.Implementation details:
Design Pattern:
Derived Class Requirements:
Reimplemented in cancellable_long_job, kcenon::thread::aging_typed_job_t< job_type >, kcenon::thread::aging_typed_job_t< job_types >, kcenon::thread::callback_job, kcenon::thread::callback_typed_job_t< job_type >, kcenon::thread::dag_job, kcenon::thread::future_job< R >, kcenon::thread::job_builder::built_job, kcenon::thread::protected_job, and non_cancellable_job.
Definition at line 135 of file job.cpp.
References kcenon::thread::not_implemented.
Referenced by basic_spsc_example(), basic_typed_queue_example(), batch_operations_example(), kcenon::thread::callback_job::do_work(), kcenon::thread::typed_thread_worker_t< job_type >::do_work(), mpmc_example(), mpmc_typed_queue_example(), performance_comparison_example(), policy_comparison_example(), and stress_test_example().

|
private |
Ensures components_ is allocated (lazy initialization).
Ensures the components structure is allocated.
Implementation details:
Definition at line 279 of file job.cpp.
|
nodiscardvirtual |
Gets the cancellation token associated with this job.
Gets the current cancellation token.
Implementation details:
Typical Usage in Derived Classes:
Definition at line 192 of file job.cpp.
References cancellation_token_.
|
inlinenodiscard |
Gets the time when this job was created (enqueued).
Definition at line 151 of file job.h.
References enqueue_time_.
|
inlinenodiscard |
|
nodiscardvirtual |
Retrieves the job_queue associated with this job, if any.
Gets the associated job queue if it still exists.
std::shared_ptr to the associated job_queue. Will be empty if no queue was set or if the queue has already been destroyed.Implementation details:
Return Value Interpretation:
Safety Considerations:
Definition at line 244 of file job.cpp.
|
nodiscard |
Retrieves the name of this job.
Gets the descriptive name of this job.
The name can be useful for logging and diagnostic messages, especially when multiple jobs are running concurrently.
Implementation details:
Definition at line 112 of file job.cpp.
Referenced by cancellable_long_job::do_work(), non_cancellable_job::do_work(), kcenon::thread::dag_job::get_info(), kcenon::thread::protected_job::get_name(), kcenon::thread::dag_scheduler::to_dot(), and kcenon::thread::dag_job::to_string().

|
nodiscard |
Gets the priority level of this job.
Implementation details:
Definition at line 400 of file job.cpp.
References components_, and kcenon::thread::normal.
|
nodiscard |
Gets the retry policy of this job.
Implementation details:
Definition at line 418 of file job.cpp.
References components_.
|
nodiscard |
Gets the timeout duration for this job.
Implementation details:
Definition at line 436 of file job.cpp.
References components_.
|
nodiscard |
Checks if this job has any composed components.
Implementation details:
Definition at line 468 of file job.cpp.
References components_.
|
nodiscard |
Checks if this job has an explicit cancellation set via composition.
Implementation details:
Definition at line 454 of file job.cpp.
References components_.
|
protected |
Invokes the completion callbacks if they are set.
This method should be called by derived classes or the job execution framework after do_work() completes. It handles invoking both the on_complete and on_error callbacks as appropriate.
| result | The result from do_work() |
Implementation details:
| result | The result from do_work() |
Definition at line 484 of file job.cpp.
Referenced by kcenon::thread::job_builder::built_job::do_work().

|
virtual |
Sets a cancellation token that can be used to cancel the job.
Sets the cancellation token for cooperative cancellation.
| token | The cancellation token to associate with this job. |
Implementation details:
Cancellation Model:
Usage Pattern:
| token | Cancellation token for cooperative cancellation |
|
virtual |
Associates this job with a specific job_queue.
Associates this job with a job queue for scheduling.
Once assigned, the job can be aware of the queue that manages it, enabling scenarios like re-enqueuing itself upon partial failure, or querying the queue for state (depending on the queue's interface).
std::weak_ptr. If the queue is destroyed, the pointer becomes invalid.Implementation details:
Weak Reference Benefits:
Use Cases:
| job_queue | Shared pointer to the job queue |
|
nodiscardvirtual |
Provides a string representation of the job for logging or debugging.
Provides a string representation of this job.
By default, this returns the job's name. Derived classes can override to include extra diagnostic details (e.g., job status, data contents, etc.).
name_).Implementation details:
Output Format:
Reimplemented in kcenon::thread::dag_job.
Definition at line 262 of file job.cpp.
References kcenon::thread::utils::formatter::format().

| auto kcenon::thread::job::with_cancellation | ( | const cancellation_token & | token | ) | -> job& |
Attaches a cancellation token to this job via composition.
The cancellation token enables cooperative cancellation of the job. The job should periodically check the token and abort if cancelled.
| token | The cancellation token to use |
Implementation details:
| token | The cancellation token to use |
Definition at line 350 of file job.cpp.
| auto kcenon::thread::job::with_on_complete | ( | std::function< void(common::VoidResult)> | callback | ) | -> job& |
Attaches a completion callback to this job.
The callback will be invoked after do_work() completes, regardless of success or failure. The callback receives the VoidResult from do_work().
| callback | Function to call with the job's result |
Implementation details:
| callback | Function to call with the job's result |
Definition at line 299 of file job.cpp.
References kcenon::thread::callback.
| auto kcenon::thread::job::with_on_error | ( | std::function< void(const common::error_info &)> | callback | ) | -> job& |
Attaches an error callback to this job.
The callback will be invoked only if do_work() returns an error. This is more specific than with_on_complete() for error-only handling.
| callback | Function to call with the error information |
Implementation details:
| callback | Function to call with the error information |
Definition at line 316 of file job.cpp.
References kcenon::thread::callback.
| auto kcenon::thread::job::with_priority | ( | job_priority | priority | ) | -> job& |
Sets the priority level for this job.
Priority can be used by job queues and schedulers to determine execution order. Higher priority jobs are typically executed first.
| priority | The priority level to set |
Implementation details:
| priority | The priority level to set |
Definition at line 333 of file job.cpp.
References kcenon::thread::priority.
| auto kcenon::thread::job::with_retry | ( | const retry_policy & | policy | ) | -> job& |
Attaches a retry policy to this job.
When the job fails, it can be automatically retried according to the specified retry policy. The executor must support retry behavior.
| policy | The retry policy to use |
Implementation details:
| policy | The retry policy to use |
| auto kcenon::thread::job::with_timeout | ( | std::chrono::milliseconds | timeout | ) | -> job& |
Sets a timeout for job execution.
If the job does not complete within the specified duration, it may be cancelled (requires executor support for timeout handling).
| timeout | Maximum execution time allowed |
Implementation details:
| timeout | Maximum execution time allowed |
|
protected |
The cancellation token associated with this job.
This token can be used to cancel the job during execution. The job should periodically check this token and abort if it is cancelled.
Definition at line 504 of file job.h.
Referenced by cancellable_long_job::do_work(), kcenon::thread::future_job< R >::do_work(), kcenon::thread::job_builder::built_job::do_work(), and get_cancellation_token().
|
private |
Composed components for this job.
Lazily allocated when first with_*() method is called. This enables the composition pattern without memory overhead for jobs that don't use composition.
Definition at line 535 of file job.h.
Referenced by get_priority(), get_retry_policy(), get_timeout(), has_components(), and has_explicit_cancellation().
|
protected |
An optional container of raw byte data that may be used by the job.
If the constructor without the data parameter is used, this vector will remain empty unless manually populated by derived classes or other means.
Definition at line 488 of file job.h.
Referenced by kcenon::thread::job_builder::built_job::do_work().
|
private |
Time when the job was created.
Captured during construction, used for wait time calculations in diagnostics.
Definition at line 526 of file job.h.
Referenced by get_enqueue_time().
|
private |
Unique identifier for this job.
Generated automatically during construction, unique within the lifetime of the application.
Definition at line 518 of file job.h.
Referenced by get_job_id().
|
protected |
|
protected |
|
staticprivate |