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

Base interface for thread pool policies. More...

#include <pool_policy.h>

Inheritance diagram for kcenon::thread::pool_policy:
Inheritance graph
Collaboration diagram for kcenon::thread::pool_policy:
Collaboration graph

Public Member Functions

virtual ~pool_policy ()=default
 Virtual destructor for proper cleanup.
 
virtual auto on_enqueue (job &j) -> common::VoidResult=0
 Called before a job is enqueued.
 
virtual void on_job_start (job &j)=0
 Called when a worker starts executing a job.
 
virtual void on_job_complete (job &j, bool success, const std::exception *error=nullptr)=0
 Called when a job completes (success or failure).
 
virtual auto get_name () const -> std::string=0
 Gets the policy name for identification and logging.
 
virtual auto is_enabled () const -> bool
 Checks if the policy is currently enabled.
 
virtual void set_enabled (bool enabled)
 Enables or disables the policy.
 

Detailed Description

Base interface for thread pool policies.

Policies provide a way to extend thread pool behavior without modifying the thread_pool class itself. This follows the Strategy pattern and Single Responsibility Principle (SRP).

Design Principles

  • Extensibility: New behaviors can be added by implementing this interface
  • Composability: Multiple policies can be combined in a thread pool
  • Non-intrusive: Policies don't require changes to core thread_pool code
  • Testability: Each policy can be unit tested independently

Lifecycle Hooks

Policies receive callbacks at key points in the job lifecycle:

Thread Safety

All methods must be thread-safe as they may be called from multiple workers.

Usage Example

class my_logging_policy : public pool_policy {
public:
auto on_enqueue(job& j) -> common::VoidResult override {
LOG_INFO("Job {} enqueued", j.get_name());
return common::ok();
}
void on_job_start(job& j) override {
LOG_INFO("Job {} started", j.get_name());
}
void on_job_complete(job& j, bool success, const std::exception* error) override {
LOG_INFO("Job {} completed: {}", j.get_name(), success ? "success" : "failed");
}
auto get_name() const -> std::string override { return "logging_policy"; }
};
pool->add_policy(std::make_unique<my_logging_policy>());
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
auto get_name(void) const -> std::string
Retrieves the name of this job.
Definition job.cpp:112
Base interface for thread pool policies.
Definition pool_policy.h:81
virtual auto on_enqueue(job &j) -> common::VoidResult=0
Called before a job is enqueued.
virtual void on_job_start(job &j)=0
Called when a worker starts executing a job.
virtual void on_job_complete(job &j, bool success, const std::exception *error=nullptr)=0
Called when a job completes (success or failure).
virtual auto get_name() const -> std::string=0
Gets the policy name for identification and logging.
@ error
Error events that might still allow continuation.
STL namespace.
See also
circuit_breaker_policy

Definition at line 80 of file pool_policy.h.

Constructor & Destructor Documentation

◆ ~pool_policy()

virtual kcenon::thread::pool_policy::~pool_policy ( )
virtualdefault

Virtual destructor for proper cleanup.

Member Function Documentation

◆ get_name()

virtual auto kcenon::thread::pool_policy::get_name ( ) const -> std::string
nodiscardpure virtual

Gets the policy name for identification and logging.

Returns
Policy name string.

Implemented in kcenon::thread::autoscaling_pool_policy, kcenon::thread::circuit_breaker_policy, and kcenon::thread::work_stealing_pool_policy.

◆ is_enabled()

virtual auto kcenon::thread::pool_policy::is_enabled ( ) const -> bool
inlinenodiscardvirtual

Checks if the policy is currently enabled.

Returns
True if policy is active.

Reimplemented in kcenon::thread::autoscaling_pool_policy, kcenon::thread::circuit_breaker_policy, and kcenon::thread::work_stealing_pool_policy.

Definition at line 146 of file pool_policy.h.

146{ return true; }

◆ on_enqueue()

virtual auto kcenon::thread::pool_policy::on_enqueue ( job & j) -> common::VoidResult
pure virtual

Called before a job is enqueued.

Parameters
jReference to the job being enqueued.
Returns
common::VoidResult - ok() to allow, error to reject the job.

Policies can use this to:

  • Validate the job
  • Apply transformations
  • Reject jobs based on policy rules (e.g., circuit breaker open)

Thread Safety:

  • Must be thread-safe
  • Called from the enqueueing thread

Implemented in kcenon::thread::autoscaling_pool_policy, kcenon::thread::circuit_breaker_policy, and kcenon::thread::work_stealing_pool_policy.

◆ on_job_complete()

virtual void kcenon::thread::pool_policy::on_job_complete ( job & j,
bool success,
const std::exception * error = nullptr )
pure virtual

Called when a job completes (success or failure).

Parameters
jReference to the completed job.
successTrue if job completed successfully.
errorPointer to exception if job failed, nullptr otherwise.

Policies can use this to:

  • Record success/failure metrics
  • Update circuit breaker state
  • Log completion

Thread Safety:

  • Must be thread-safe
  • Called from the worker thread

Implemented in kcenon::thread::autoscaling_pool_policy, kcenon::thread::circuit_breaker_policy, and kcenon::thread::work_stealing_pool_policy.

◆ on_job_start()

virtual void kcenon::thread::pool_policy::on_job_start ( job & j)
pure virtual

Called when a worker starts executing a job.

Parameters
jReference to the job being started.

Policies can use this to:

  • Start timing
  • Update metrics
  • Log job start

Thread Safety:

  • Must be thread-safe
  • Called from the worker thread

Implemented in kcenon::thread::autoscaling_pool_policy, kcenon::thread::circuit_breaker_policy, and kcenon::thread::work_stealing_pool_policy.

◆ set_enabled()

virtual void kcenon::thread::pool_policy::set_enabled ( bool enabled)
inlinevirtual

Enables or disables the policy.

Parameters
enabledWhether to enable the policy.

Disabled policies have their hooks called but should no-op.

Reimplemented in kcenon::thread::autoscaling_pool_policy, kcenon::thread::circuit_breaker_policy, and kcenon::thread::work_stealing_pool_policy.

Definition at line 154 of file pool_policy.h.

154{ (void)enabled; }

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