ThreadSchedule 2.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
registered_threads.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include "pthread_wrapper.hpp"
9#include "thread_registry.hpp"
10#include "thread_wrapper.hpp"
11#include <functional>
12#include <string>
13#include <type_traits>
14#include <utility>
15
16namespace threadschedule
17{
18
29{
30 public:
31 ThreadWrapperReg() = default;
32
33 ThreadWrapperReg(ThreadWrapperReg&&) noexcept = default;
34 auto operator=(ThreadWrapperReg&&) noexcept -> ThreadWrapperReg& = default;
35
37 auto operator=(ThreadWrapperReg const&) -> ThreadWrapperReg& = delete;
38
39 template <typename F, typename... Args>
40 explicit ThreadWrapperReg(std::string name, std::string componentTag, F&& f, Args&&... args)
42 [n = std::move(name), c = std::move(componentTag), func = std::forward<F>(f)](auto&&... inner) {
43 AutoRegisterCurrentThread guard(n, c);
44 std::invoke(func, std::forward<decltype(inner)>(inner)...);
45 },
46 std::forward<Args>(args)...)
47 {
48 }
49};
50
51#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
61class JThreadWrapperReg : public JThreadWrapper
62{
63 public:
64 JThreadWrapperReg() = default;
65
66 JThreadWrapperReg(JThreadWrapperReg&&) noexcept = default;
67 auto operator=(JThreadWrapperReg&&) noexcept -> JThreadWrapperReg& = default;
68
69 JThreadWrapperReg(JThreadWrapperReg const&) = delete;
70 auto operator=(JThreadWrapperReg const&) -> JThreadWrapperReg& = delete;
71
72 template <typename F, typename... Args>
73 explicit JThreadWrapperReg(std::string name, std::string componentTag, F&& f, Args&&... args)
74 : JThreadWrapper(
75 [n = std::move(name), c = std::move(componentTag),
76 func = std::forward<F>(f)](std::stop_token st, auto&&... inner) {
77 AutoRegisterCurrentThread guard(n, c);
78 if constexpr (std::is_invocable_v<decltype(func), std::stop_token,
79 decltype(inner)...>)
80 std::invoke(func, std::move(st), std::forward<decltype(inner)>(inner)...);
81 else if constexpr (std::is_invocable_v<decltype(func), decltype(inner)...,
82 std::stop_token>)
83 std::invoke(func, std::forward<decltype(inner)>(inner)..., std::move(st));
84 else
85 std::invoke(func, std::forward<decltype(inner)>(inner)...);
86 },
87 std::forward<Args>(args)...)
88 {
89 }
90};
91#endif
92
93#ifndef _WIN32
102{
103 public:
104 PThreadWrapperReg() = default;
105
107 auto operator=(PThreadWrapperReg&&) noexcept -> PThreadWrapperReg& = default;
108
110 auto operator=(PThreadWrapperReg const&) -> PThreadWrapperReg& = delete;
111
112 template <typename F, typename... Args>
113 explicit PThreadWrapperReg(std::string name, std::string componentTag, F&& f, Args&&... args)
115 [n = std::move(name), c = std::move(componentTag), func = std::forward<F>(f)](auto&&... inner) {
116 AutoRegisterCurrentThread guard(n, c);
117 std::invoke(func, std::forward<decltype(inner)>(inner)...);
118 },
119 std::forward<Args>(args)...)
120 {
121 }
122};
123#endif
124
125} // namespace threadschedule
RAII guard that registers the current thread on construction and unregisters it on destruction.
PThreadWrapperReg(PThreadWrapperReg &&) noexcept=default
ThreadWrapperReg(ThreadWrapperReg &&) noexcept=default
ThreadWrapper JThreadWrapper
Owning wrapper around std::jthread with cooperative cancellation (C++20).
RAII wrapper around POSIX threads (Linux only).
Process-wide thread registry, control blocks, and composite registry.
Enhanced thread wrappers: ThreadWrapper, JThreadWrapper, and non-owning views.