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

Pool policy that implements work-stealing behavior for load balancing. More...

#include <work_stealing_pool_policy.h>

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

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_policyoperator= (const work_stealing_pool_policy &)=delete
 
 work_stealing_pool_policy (work_stealing_pool_policy &&)=delete
 
work_stealing_pool_policyoperator= (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}
 

Detailed Description

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 Pattern

Work-stealing enables idle workers to "steal" jobs from busy workers' local queues, improving load balancing and throughput:

  • Workers first check their local queue for work
  • If empty, they attempt to steal from other workers
  • Victim selection can be random, round-robin, or adaptive

Thread Safety

All methods are thread-safe and can be called from any thread.

Usage Example

config.enable_work_stealing = true;
config.max_steal_attempts = 5;
auto pool = std::make_shared<thread_pool>("my_pool");
auto ws_policy = std::make_unique<work_stealing_pool_policy>(config);
pool->add_policy(std::move(ws_policy));
pool->start();
@ adaptive
Select based on queue sizes (best for uneven loads)
Worker behavior policy configuration.
See also
pool_policy
worker_policy

Definition at line 62 of file work_stealing_pool_policy.h.

Constructor & Destructor Documentation

◆ work_stealing_pool_policy() [1/3]

kcenon::thread::work_stealing_pool_policy::work_stealing_pool_policy ( const worker_policy & config = {})
explicit

Constructs a work-stealing policy with the given configuration.

Parameters
configWorker policy configuration containing work-stealing settings.

Definition at line 11 of file work_stealing_pool_policy.cpp.

12 : policy_(config)
13 , enabled_(config.enable_work_stealing)
14{
15}

◆ ~work_stealing_pool_policy()

kcenon::thread::work_stealing_pool_policy::~work_stealing_pool_policy ( )
overridedefault

Destructor.

◆ work_stealing_pool_policy() [2/3]

kcenon::thread::work_stealing_pool_policy::work_stealing_pool_policy ( const work_stealing_pool_policy & )
delete

◆ work_stealing_pool_policy() [3/3]

kcenon::thread::work_stealing_pool_policy::work_stealing_pool_policy ( work_stealing_pool_policy && )
delete

Member Function Documentation

◆ get_failed_steals()

auto kcenon::thread::work_stealing_pool_policy::get_failed_steals ( ) const -> std::uint64_t
nodiscard

Gets the total number of failed steal attempts.

Returns
Number of failed steal attempts.

Definition at line 98 of file work_stealing_pool_policy.cpp.

99{
100 return failed_steals_.load(std::memory_order_acquire);
101}

References failed_steals_.

◆ get_max_steal_attempts()

auto kcenon::thread::work_stealing_pool_policy::get_max_steal_attempts ( ) const -> std::size_t
nodiscard

Gets the maximum steal attempts per steal cycle.

Returns
Maximum steal attempts.

Definition at line 73 of file work_stealing_pool_policy.cpp.

74{
76}

References kcenon::thread::worker_policy::max_steal_attempts, and policy_.

◆ get_name()

auto kcenon::thread::work_stealing_pool_policy::get_name ( ) const -> std::string
nodiscardoverridevirtual

Gets the policy name.

Returns
"work_stealing_pool_policy"

Implements kcenon::thread::pool_policy.

Definition at line 36 of file work_stealing_pool_policy.cpp.

37{
38 return "work_stealing_pool_policy";
39}

◆ get_policy()

auto kcenon::thread::work_stealing_pool_policy::get_policy ( ) const -> const worker_policy&
nodiscard

Gets the current worker policy configuration.

Returns
Const reference to the worker policy.

Definition at line 52 of file work_stealing_pool_policy.cpp.

53{
54 return policy_;
55}

References policy_.

◆ get_steal_backoff()

auto kcenon::thread::work_stealing_pool_policy::get_steal_backoff ( ) const -> std::chrono::microseconds
nodiscard

Gets the steal backoff duration.

Returns
Backoff duration between steal attempts.

Definition at line 83 of file work_stealing_pool_policy.cpp.

84{
86}
std::chrono::microseconds steal_backoff

References policy_, and kcenon::thread::worker_policy::steal_backoff.

◆ get_steal_policy()

auto kcenon::thread::work_stealing_pool_policy::get_steal_policy ( ) const -> steal_policy
nodiscard

Gets the steal policy (victim selection strategy).

Returns
Current steal policy.

Definition at line 63 of file work_stealing_pool_policy.cpp.

64{
66}

References policy_, and kcenon::thread::worker_policy::victim_selection.

◆ get_successful_steals()

auto kcenon::thread::work_stealing_pool_policy::get_successful_steals ( ) const -> std::uint64_t
nodiscard

Gets the total number of successful steals.

Returns
Number of successful steal operations.

Definition at line 93 of file work_stealing_pool_policy.cpp.

94{
95 return successful_steals_.load(std::memory_order_acquire);
96}

References successful_steals_.

◆ is_enabled()

auto kcenon::thread::work_stealing_pool_policy::is_enabled ( ) const -> bool
nodiscardoverridevirtual

Checks if the policy is enabled.

Returns
True if work-stealing is enabled.

Reimplemented from kcenon::thread::pool_policy.

Definition at line 41 of file work_stealing_pool_policy.cpp.

42{
43 return enabled_.load(std::memory_order_acquire);
44}

References enabled_.

◆ on_enqueue()

auto kcenon::thread::work_stealing_pool_policy::on_enqueue ( job & j) -> common::VoidResult
overridevirtual

Called before a job is enqueued.

Parameters
jReference to the job being enqueued.
Returns
common::ok() - work stealing does not reject jobs.

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.

18{
19 (void)j; // Job not used, work-stealing doesn't affect enqueue
20 return common::ok();
21}

◆ on_job_complete()

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

Called when a job completes.

Parameters
jReference to the completed job.
successTrue if job succeeded.
errorException 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.

29{
30 (void)j; // Job not used for work-stealing statistics
31 (void)success;
32 (void)error;
33 // Statistics are tracked through record_successful_steal/record_failed_steal
34}
@ error
Error events that might still allow continuation.

References kcenon::thread::success.

◆ on_job_start()

void kcenon::thread::work_stealing_pool_policy::on_job_start ( job & j)
overridevirtual

Called when job starts executing.

Parameters
jReference 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.

24{
25 (void)j; // Currently no action needed on job start
26}

◆ operator=() [1/2]

work_stealing_pool_policy & kcenon::thread::work_stealing_pool_policy::operator= ( const work_stealing_pool_policy & )
delete

◆ operator=() [2/2]

work_stealing_pool_policy & kcenon::thread::work_stealing_pool_policy::operator= ( work_stealing_pool_policy && )
delete

◆ record_failed_steal()

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.

115{
116 failed_steals_.fetch_add(1, std::memory_order_relaxed);
117}

References failed_steals_.

◆ record_successful_steal()

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.

110{
111 successful_steals_.fetch_add(1, std::memory_order_relaxed);
112}

References successful_steals_.

◆ reset_stats()

void kcenon::thread::work_stealing_pool_policy::reset_stats ( )

Resets the steal statistics.

Definition at line 103 of file work_stealing_pool_policy.cpp.

104{
105 successful_steals_.store(0, std::memory_order_release);
106 failed_steals_.store(0, std::memory_order_release);
107}

References failed_steals_, and successful_steals_.

◆ set_enabled()

void kcenon::thread::work_stealing_pool_policy::set_enabled ( bool enabled)
overridevirtual

Enables or disables the policy.

Parameters
enabledWhether to enable work-stealing.

Reimplemented from kcenon::thread::pool_policy.

Definition at line 46 of file work_stealing_pool_policy.cpp.

47{
48 enabled_.store(enabled, std::memory_order_release);
50}

References kcenon::thread::worker_policy::enable_work_stealing, enabled_, and policy_.

◆ set_max_steal_attempts()

void kcenon::thread::work_stealing_pool_policy::set_max_steal_attempts ( std::size_t attempts)

Sets the maximum steal attempts per steal cycle.

Parameters
attemptsNew maximum steal attempts.

Definition at line 78 of file work_stealing_pool_policy.cpp.

79{
80 policy_.max_steal_attempts = attempts;
81}

References kcenon::thread::worker_policy::max_steal_attempts, and policy_.

◆ set_policy()

void kcenon::thread::work_stealing_pool_policy::set_policy ( const worker_policy & config)

Updates the worker policy configuration.

Parameters
configNew worker policy configuration.

Note: Changes take effect for subsequent operations.

Definition at line 57 of file work_stealing_pool_policy.cpp.

58{
59 policy_ = config;
60 enabled_.store(config.enable_work_stealing, std::memory_order_release);
61}

References kcenon::thread::worker_policy::enable_work_stealing, enabled_, and policy_.

◆ set_steal_backoff()

void kcenon::thread::work_stealing_pool_policy::set_steal_backoff ( std::chrono::microseconds backoff)

Sets the steal backoff duration.

Parameters
backoffNew backoff duration.

Definition at line 88 of file work_stealing_pool_policy.cpp.

89{
90 policy_.steal_backoff = backoff;
91}

References policy_, and kcenon::thread::worker_policy::steal_backoff.

◆ set_steal_policy()

void kcenon::thread::work_stealing_pool_policy::set_steal_policy ( steal_policy policy)

Sets the steal policy (victim selection strategy).

Parameters
policyNew steal policy.

Definition at line 68 of file work_stealing_pool_policy.cpp.

69{
71}

References policy_, and kcenon::thread::worker_policy::victim_selection.

Member Data Documentation

◆ enabled_

std::atomic<bool> kcenon::thread::work_stealing_pool_policy::enabled_
private

Definition at line 221 of file work_stealing_pool_policy.h.

Referenced by is_enabled(), set_enabled(), and set_policy().

◆ failed_steals_

std::atomic<std::uint64_t> kcenon::thread::work_stealing_pool_policy::failed_steals_ {0}
private

Definition at line 223 of file work_stealing_pool_policy.h.

223{0};

Referenced by get_failed_steals(), record_failed_steal(), and reset_stats().

◆ policy_

◆ successful_steals_

std::atomic<std::uint64_t> kcenon::thread::work_stealing_pool_policy::successful_steals_ {0}
private

Definition at line 222 of file work_stealing_pool_policy.h.

222{0};

Referenced by get_successful_steals(), record_successful_steal(), and reset_stats().


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