|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Pool policy that implements work-stealing behavior for load balancing. More...
#include <work_stealing_pool_policy.h>


Public Member Functions | |
| work_stealing_pool_policy (const worker_policy &config={}) | |
| Constructs a work-stealing policy with the given configuration. | |
| ~work_stealing_pool_policy () override=default | |
| Destructor. | |
| work_stealing_pool_policy (const work_stealing_pool_policy &)=delete | |
| work_stealing_pool_policy & | operator= (const work_stealing_pool_policy &)=delete |
| work_stealing_pool_policy (work_stealing_pool_policy &&)=delete | |
| work_stealing_pool_policy & | operator= (work_stealing_pool_policy &&)=delete |
| auto | on_enqueue (job &j) -> common::VoidResult override |
| Called before a job is enqueued. | |
| void | on_job_start (job &j) override |
| Called when job starts executing. | |
| void | on_job_complete (job &j, bool success, const std::exception *error=nullptr) override |
| Called when a job completes. | |
| auto | get_name () const -> std::string override |
| Gets the policy name. | |
| auto | is_enabled () const -> bool override |
| Checks if the policy is enabled. | |
| void | set_enabled (bool enabled) override |
| Enables or disables the policy. | |
| auto | get_policy () const -> const worker_policy & |
| Gets the current worker policy configuration. | |
| void | set_policy (const worker_policy &config) |
| Updates the worker policy configuration. | |
| auto | get_steal_policy () const -> steal_policy |
| Gets the steal policy (victim selection strategy). | |
| void | set_steal_policy (steal_policy policy) |
| Sets the steal policy (victim selection strategy). | |
| auto | get_max_steal_attempts () const -> std::size_t |
| Gets the maximum steal attempts per steal cycle. | |
| void | set_max_steal_attempts (std::size_t attempts) |
| Sets the maximum steal attempts per steal cycle. | |
| auto | get_steal_backoff () const -> std::chrono::microseconds |
| Gets the steal backoff duration. | |
| void | set_steal_backoff (std::chrono::microseconds backoff) |
| Sets the steal backoff duration. | |
| auto | get_successful_steals () const -> std::uint64_t |
| Gets the total number of successful steals. | |
| auto | get_failed_steals () const -> std::uint64_t |
| Gets the total number of failed steal attempts. | |
| void | reset_stats () |
| Resets the steal statistics. | |
| void | record_successful_steal () |
| Records a successful steal operation. | |
| void | record_failed_steal () |
| Records a failed steal attempt. | |
Public Member Functions inherited from kcenon::thread::pool_policy | |
| virtual | ~pool_policy ()=default |
| Virtual destructor for proper cleanup. | |
Private Attributes | |
| worker_policy | policy_ |
| std::atomic< bool > | enabled_ |
| std::atomic< std::uint64_t > | successful_steals_ {0} |
| std::atomic< std::uint64_t > | failed_steals_ {0} |
Pool policy that implements work-stealing behavior for load balancing.
This policy wraps the work-stealing functionality as a composable pool policy, enabling work-stealing configuration without modifying the thread_pool class.
Work-stealing enables idle workers to "steal" jobs from busy workers' local queues, improving load balancing and throughput:
All methods are thread-safe and can be called from any thread.
Definition at line 62 of file work_stealing_pool_policy.h.
|
explicit |
Constructs a work-stealing policy with the given configuration.
| config | Worker policy configuration containing work-stealing settings. |
Definition at line 11 of file work_stealing_pool_policy.cpp.
|
overridedefault |
Destructor.
|
delete |
|
delete |
|
nodiscard |
Gets the total number of failed steal attempts.
Definition at line 98 of file work_stealing_pool_policy.cpp.
References failed_steals_.
|
nodiscard |
Gets the maximum steal attempts per steal cycle.
Definition at line 73 of file work_stealing_pool_policy.cpp.
References kcenon::thread::worker_policy::max_steal_attempts, and policy_.
|
nodiscardoverridevirtual |
Gets the policy name.
Implements kcenon::thread::pool_policy.
Definition at line 36 of file work_stealing_pool_policy.cpp.
|
nodiscard |
Gets the current worker policy configuration.
Definition at line 52 of file work_stealing_pool_policy.cpp.
References policy_.
|
nodiscard |
Gets the steal backoff duration.
Definition at line 83 of file work_stealing_pool_policy.cpp.
References policy_, and kcenon::thread::worker_policy::steal_backoff.
|
nodiscard |
Gets the steal policy (victim selection strategy).
Definition at line 63 of file work_stealing_pool_policy.cpp.
References policy_, and kcenon::thread::worker_policy::victim_selection.
|
nodiscard |
Gets the total number of successful steals.
Definition at line 93 of file work_stealing_pool_policy.cpp.
References successful_steals_.
|
nodiscardoverridevirtual |
Checks if the policy is enabled.
Reimplemented from kcenon::thread::pool_policy.
Definition at line 41 of file work_stealing_pool_policy.cpp.
References enabled_.
|
overridevirtual |
Called before a job is enqueued.
| j | Reference to the job being enqueued. |
Work-stealing policy does not modify enqueue behavior. Jobs are always accepted.
Implements kcenon::thread::pool_policy.
Definition at line 17 of file work_stealing_pool_policy.cpp.
|
overridevirtual |
Called when a job completes.
| j | Reference to the completed job. |
| success | True if job succeeded. |
| error | Exception pointer if job failed. |
Updates internal statistics for adaptive stealing.
Implements kcenon::thread::pool_policy.
Definition at line 28 of file work_stealing_pool_policy.cpp.
References kcenon::thread::success.
|
overridevirtual |
Called when job starts executing.
| j | Reference to the job. |
Can be used to track job execution for steal decisions.
Implements kcenon::thread::pool_policy.
Definition at line 23 of file work_stealing_pool_policy.cpp.
|
delete |
|
delete |
| void kcenon::thread::work_stealing_pool_policy::record_failed_steal | ( | ) |
Records a failed steal attempt.
Call this from the thread_pool when a steal fails.
Definition at line 114 of file work_stealing_pool_policy.cpp.
References failed_steals_.
| void kcenon::thread::work_stealing_pool_policy::record_successful_steal | ( | ) |
Records a successful steal operation.
Call this from the thread_pool when a steal succeeds.
Definition at line 109 of file work_stealing_pool_policy.cpp.
References successful_steals_.
| void kcenon::thread::work_stealing_pool_policy::reset_stats | ( | ) |
Resets the steal statistics.
Definition at line 103 of file work_stealing_pool_policy.cpp.
References failed_steals_, and successful_steals_.
|
overridevirtual |
Enables or disables the policy.
| enabled | Whether to enable work-stealing. |
Reimplemented from kcenon::thread::pool_policy.
Definition at line 46 of file work_stealing_pool_policy.cpp.
References kcenon::thread::worker_policy::enable_work_stealing, enabled_, and policy_.
| void kcenon::thread::work_stealing_pool_policy::set_max_steal_attempts | ( | std::size_t | attempts | ) |
Sets the maximum steal attempts per steal cycle.
| attempts | New maximum steal attempts. |
Definition at line 78 of file work_stealing_pool_policy.cpp.
References kcenon::thread::worker_policy::max_steal_attempts, and policy_.
| void kcenon::thread::work_stealing_pool_policy::set_policy | ( | const worker_policy & | config | ) |
Updates the worker policy configuration.
| config | New worker policy configuration. |
Note: Changes take effect for subsequent operations.
Definition at line 57 of file work_stealing_pool_policy.cpp.
References kcenon::thread::worker_policy::enable_work_stealing, enabled_, and policy_.
| void kcenon::thread::work_stealing_pool_policy::set_steal_backoff | ( | std::chrono::microseconds | backoff | ) |
Sets the steal backoff duration.
| backoff | New backoff duration. |
Definition at line 88 of file work_stealing_pool_policy.cpp.
References policy_, and kcenon::thread::worker_policy::steal_backoff.
| void kcenon::thread::work_stealing_pool_policy::set_steal_policy | ( | steal_policy | policy | ) |
Sets the steal policy (victim selection strategy).
| policy | New steal policy. |
Definition at line 68 of file work_stealing_pool_policy.cpp.
References policy_, and kcenon::thread::worker_policy::victim_selection.
|
private |
Definition at line 221 of file work_stealing_pool_policy.h.
Referenced by is_enabled(), set_enabled(), and set_policy().
|
private |
Definition at line 223 of file work_stealing_pool_policy.h.
Referenced by get_failed_steals(), record_failed_steal(), and reset_stats().
|
private |
Definition at line 220 of file work_stealing_pool_policy.h.
Referenced by get_max_steal_attempts(), get_policy(), get_steal_backoff(), get_steal_policy(), set_enabled(), set_max_steal_attempts(), set_policy(), set_steal_backoff(), and set_steal_policy().
|
private |
Definition at line 222 of file work_stealing_pool_policy.h.
Referenced by get_successful_steals(), record_successful_steal(), and reset_stats().