Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
typed_thread_worker.cpp
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
6
7namespace kcenon::thread
8{
9 template <typename job_type>
11 std::vector<job_type> types,
12 const bool& use_time_tag,
13 const thread_context& context)
14 : thread_base()
15 , use_time_tag_(use_time_tag)
16 , types_(std::move(types))
17 , job_queue_(nullptr)
18 , context_(context)
19 {
20 }
21
22 template <typename job_type>
26
27 template <typename job_type>
29 std::shared_ptr<aging_typed_job_queue_t<job_type>> job_queue) -> void
30 {
31 job_queue_ = std::move(job_queue);
32 }
33
34 template <typename job_type>
35 auto typed_thread_worker_t<job_type>::types() const -> std::vector<job_type>
36 {
37 return types_;
38 }
39
40 template <typename job_type>
42 {
43 context_ = context;
44 }
45
46 template <typename job_type>
48 {
49 return context_;
50 }
51
52 template <typename job_type>
54 {
55 if (!job_queue_)
56 {
57 return false;
58 }
59
60 return !job_queue_->empty(types_);
61 }
62
63 template <typename job_type>
64 auto typed_thread_worker_t<job_type>::do_work() -> common::VoidResult
65 {
66 if (!job_queue_)
67 {
68 return common::ok(); // No queue available
69 }
70
71 // Dequeue a job matching the worker's priority levels
72 auto job_result = job_queue_->dequeue(types_);
73 if (job_result.is_err())
74 {
75 return common::ok(); // No job available or error occurred
76 }
77
78 auto job = std::move(job_result.value());
79 if (!job)
80 {
81 return common::ok(); // No job to process
82 }
83
84 // Execute the job
85 return job->do_work();
86 }
87
88 // Explicit template instantiation for job_types
90
91} // namespace kcenon::thread
A typed job queue with priority aging support, based on policy_queue.
A thread-safe job queue for managing and dispatching work items.
Definition job_queue.h:65
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
virtual auto do_work(void) -> common::VoidResult
The core task execution method to be overridden by derived classes.
Definition job.cpp:135
A foundational class for implementing custom worker threads.
Context object that provides access to optional services.
auto do_work() -> common::VoidResult override
Executes pending work by processing one job at a time.
auto set_aging_job_queue(std::shared_ptr< aging_typed_job_queue_t< job_type > > job_queue) -> void
Assigns a priority job queue to this worker.
auto should_continue_work() const -> bool override
Determines if there is any pending work for this worker.
typed_thread_worker_t(std::vector< job_type > types=get_all_job_types(), const bool &use_time_tag=true, const thread_context &context=thread_context())
Constructs a new typed_thread_worker_t object.
virtual ~typed_thread_worker_t(void)
Virtual destructor for the typed_thread_worker_t class.
auto get_context(void) const -> const thread_context &
Gets the thread context for this worker.
auto set_context(const thread_context &context) -> void
Sets the thread context for this worker.
auto types(void) const -> std::vector< job_type >
Retrieves the list of priority levels this worker handles.
Worker thread for typed_thread_pool processing priority job queues.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.