Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
worker_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
5#pragma once
6
16#include "config.h"
17#include <chrono>
18#include <string>
19
20namespace kcenon::thread {
21
25 enum class worker_state {
26 created,
27 starting,
28 active,
29 idle,
30 stopping,
31 stopped
32 };
33
37 enum class scheduling_policy {
38 fifo,
39 lifo,
40 priority,
42 };
43
50 enum class steal_policy {
51 random,
54 };
55
63 // Scheduling behavior
65
66 // Idle behavior
69 bool sleep_when_idle = true;
70 std::chrono::microseconds idle_sleep_duration{100};
71
72 // Work stealing behavior
76 std::chrono::microseconds steal_backoff{50};
77
78 // Performance behavior
80 int preferred_cpu = -1; // -1 means no preference
81 size_t max_jobs_per_batch = 10;
82
83 // Error handling
86
87 // Debugging and monitoring
90
95 return worker_policy{};
96 }
97
102 worker_policy policy;
103 policy.yield_on_idle = false;
104 policy.sleep_when_idle = false;
105 policy.enable_work_stealing = true;
106 policy.max_jobs_per_batch = 20;
107 return policy;
108 }
109
114 worker_policy policy;
116 policy.yield_on_idle = false;
117 policy.idle_sleep_duration = std::chrono::microseconds{10};
118 policy.max_jobs_per_batch = 1;
119 return policy;
120 }
121
126 worker_policy policy;
127 policy.yield_on_idle = true;
128 policy.sleep_when_idle = true;
129 policy.idle_sleep_duration = std::chrono::milliseconds{1};
130 policy.enable_work_stealing = false;
131 return policy;
132 }
133 };
134
138 constexpr const char* to_string(worker_state state) {
139 switch (state) {
140 case worker_state::created: return "created";
141 case worker_state::starting: return "starting";
142 case worker_state::active: return "active";
143 case worker_state::idle: return "idle";
144 case worker_state::stopping: return "stopping";
145 case worker_state::stopped: return "stopped";
146 default: return "unknown";
147 }
148 }
149
153 constexpr const char* to_string(scheduling_policy policy) {
154 switch (policy) {
155 case scheduling_policy::fifo: return "fifo";
156 case scheduling_policy::lifo: return "lifo";
157 case scheduling_policy::priority: return "priority";
158 case scheduling_policy::work_stealing: return "work_stealing";
159 default: return "unknown";
160 }
161 }
162
166 constexpr const char* to_string(steal_policy policy) {
167 switch (policy) {
168 case steal_policy::random: return "random";
169 case steal_policy::round_robin: return "round_robin";
170 case steal_policy::adaptive: return "adaptive";
171 default: return "unknown";
172 }
173 }
174
175} // namespace kcenon::thread
Central configuration for thread_pool module.
Forward declarations for thread system types.
@ adaptive
Automatically adjust based on load conditions.
constexpr auto default_worker_idle_timeout
Definition config.h:32
constexpr bool default_work_stealing
Enable work stealing for improved load balancing.
Definition config.h:61
constexpr bool default_pin_threads
Definition config.h:76
constexpr bool enable_statistics
Definition config.h:85
constexpr const char * default_thread_prefix
Definition config.h:89
constexpr bool default_yield_on_idle
Definition config.h:35
Core threading foundation of the thread system library.
Definition thread_impl.h:17
worker_state
Enumeration of worker states.
@ starting
Worker is starting up.
@ stopping
Worker is shutting down.
@ active
Worker is actively processing jobs.
@ created
Worker created but not started.
@ idle
Worker is idle, waiting for work.
@ stopped
Worker has stopped.
steal_policy
Policy for selecting steal victims.
@ round_robin
Sequential victim selection (deterministic, fair)
@ random
Random victim selection (default, good load distribution)
@ adaptive
Select based on queue sizes (best for uneven loads)
scheduling_policy
Enumeration of scheduling policies.
@ work_stealing
Work-stealing scheduling.
@ lifo
Last-in, first-out scheduling.
@ priority
Priority-based scheduling.
@ fifo
First-in, first-out scheduling.
constexpr std::string_view to_string(log_level_v2 level) noexcept
Convert log_level_v2 to string representation.
Definition log_level.h:40
Worker behavior policy configuration.
std::chrono::microseconds steal_backoff
static worker_policy power_efficient()
Create a power-efficient worker policy.
static worker_policy high_performance()
Create a high-performance worker policy.
std::chrono::microseconds idle_sleep_duration
static worker_policy default_policy()
Create a default worker policy.
std::chrono::milliseconds idle_timeout
static worker_policy low_latency()
Create a low-latency worker policy.