Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
work_stealing_pool_policy.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
14#include "pool_policy.h"
16
17#include <atomic>
18#include <memory>
19#include <string>
20
21namespace kcenon::thread
22{
23 // Forward declarations
24 class thread_pool;
25
63 {
64 public:
69 explicit work_stealing_pool_policy(const worker_policy& config = {});
70
74 ~work_stealing_pool_policy() override = default;
75
76 // Non-copyable
79
80 // Non-movable (std::atomic is not movable)
83
84 // ============================================
85 // pool_policy Interface
86 // ============================================
87
96 auto on_enqueue(job& j) -> common::VoidResult override;
97
104 void on_job_start(job& j) override;
105
114 void on_job_complete(job& j, bool success, const std::exception* error = nullptr) override;
115
120 [[nodiscard]] auto get_name() const -> std::string override;
121
126 [[nodiscard]] auto is_enabled() const -> bool override;
127
132 void set_enabled(bool enabled) override;
133
134 // ============================================
135 // Work-Stealing Specific Methods
136 // ============================================
137
142 [[nodiscard]] auto get_policy() const -> const worker_policy&;
143
150 void set_policy(const worker_policy& config);
151
156 [[nodiscard]] auto get_steal_policy() const -> steal_policy;
157
162 void set_steal_policy(steal_policy policy);
163
168 [[nodiscard]] auto get_max_steal_attempts() const -> std::size_t;
169
174 void set_max_steal_attempts(std::size_t attempts);
175
180 [[nodiscard]] auto get_steal_backoff() const -> std::chrono::microseconds;
181
186 void set_steal_backoff(std::chrono::microseconds backoff);
187
192 [[nodiscard]] auto get_successful_steals() const -> std::uint64_t;
193
198 [[nodiscard]] auto get_failed_steals() const -> std::uint64_t;
199
203 void reset_stats();
204
211
217 void record_failed_steal();
218
219 private:
221 std::atomic<bool> enabled_;
222 std::atomic<std::uint64_t> successful_steals_{0};
223 std::atomic<std::uint64_t> failed_steals_{0};
224 };
225
226} // namespace kcenon::thread
Represents an error in the thread system.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
Base interface for thread pool policies.
Definition pool_policy.h:81
Pool policy that implements work-stealing behavior for load balancing.
void set_enabled(bool enabled) override
Enables or disables the policy.
work_stealing_pool_policy & operator=(work_stealing_pool_policy &&)=delete
void set_steal_backoff(std::chrono::microseconds backoff)
Sets the steal backoff duration.
work_stealing_pool_policy(work_stealing_pool_policy &&)=delete
void set_steal_policy(steal_policy policy)
Sets the steal policy (victim selection strategy).
void set_max_steal_attempts(std::size_t attempts)
Sets the maximum steal attempts per steal cycle.
void set_policy(const worker_policy &config)
Updates the worker policy configuration.
void record_failed_steal()
Records a failed steal attempt.
~work_stealing_pool_policy() override=default
Destructor.
auto get_steal_backoff() const -> std::chrono::microseconds
Gets the steal backoff duration.
auto get_successful_steals() const -> std::uint64_t
Gets the total number of successful steals.
auto is_enabled() const -> bool override
Checks if the policy is enabled.
auto get_steal_policy() const -> steal_policy
Gets the steal policy (victim selection strategy).
auto get_policy() const -> const worker_policy &
Gets the current worker policy configuration.
void on_job_complete(job &j, bool success, const std::exception *error=nullptr) override
Called when a job completes.
auto on_enqueue(job &j) -> common::VoidResult override
Called before a job is enqueued.
auto get_name() const -> std::string override
Gets the policy name.
work_stealing_pool_policy(const worker_policy &config={})
Constructs a work-stealing policy with the given configuration.
void record_successful_steal()
Records a successful steal operation.
work_stealing_pool_policy & operator=(const work_stealing_pool_policy &)=delete
work_stealing_pool_policy(const work_stealing_pool_policy &)=delete
void on_job_start(job &j) override
Called when job starts executing.
auto get_failed_steals() const -> std::uint64_t
Gets the total number of failed steal attempts.
auto get_max_steal_attempts() const -> std::size_t
Gets the maximum steal attempts per steal cycle.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
steal_policy
Policy for selecting steal victims.
STL namespace.
Base interface for extensible thread pool behavior policies.
Worker behavior policy configuration.
Worker behavior policies and configuration.