Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
callback_typed_job.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 "typed_job.h"
15
16namespace kcenon::thread
17{
30 template <typename job_type>
31 class callback_typed_job_t : public typed_job_t<job_type>
32 {
33 public:
56 callback_typed_job_t(const std::function<common::VoidResult(void)>& callback,
57 job_type priority,
58 const std::string& name = "typed_job");
59
63 ~callback_typed_job_t(void) override;
64
77 [[nodiscard]] auto do_work(void) -> common::VoidResult override;
78
79 private:
87 std::function<common::VoidResult(void)> callback_;
88 };
89
96
97 // Template definitions are provided in-header to support external priority types.
98 template <typename job_type>
100 const std::function<common::VoidResult(void)>& callback,
101 job_type priority,
102 const std::string& name)
103 : typed_job_t<job_type>(priority, name)
104 , callback_(callback)
105 {
106 }
107
108 template <typename job_type>
110
111 template <typename job_type>
112 auto callback_typed_job_t<job_type>::do_work() -> common::VoidResult
113 {
114 if (!callback_)
115 {
116 return common::error_info{static_cast<int>(error_code::invalid_argument), "Callback is null", "thread_system"};
117 }
118
119 return callback_();
120 }
121
122 // Keep a single explicit instantiation for the default job_types in the .cpp translation unit.
123 extern template class callback_typed_job_t<job_types>;
124} // namespace kcenon::thread
Callback-based typed job template.
callback_typed_job_t(const std::function< common::VoidResult(void)> &callback, job_type priority, const std::string &name="typed_job")
Constructs a new callback_typed_job_t with a callback, priority, and name.
~callback_typed_job_t(void) override
Virtual destructor for the callback_typed_job_t class.
std::function< common::VoidResult(void)> callback_
The user-provided callback function to execute when the job is processed.
auto do_work(void) -> common::VoidResult override
Executes the stored callback function for this job.
Typed job template.
Definition typed_job.h:31
auto priority() const -> job_type
Retrieves the priority level of this job.
Definition typed_job.h:55
@ callback
Call user callback for custom decision.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
@ priority
Priority-based scheduling.
Base typed job carrying a specific priority level.