ThreadSchedule 4.1.0
Modern C++ thread management library
Loading...
Searching...
No Matches
thread_view.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include "detail/lifetime.hpp"
11#include "jthread.hpp"
12#include "thread.hpp"
13#include "thread_config.hpp"
14
15#include <string>
16#include <thread>
17#include <utility>
18#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
19# include <variant>
20#endif
21
22namespace threadschedule
23{
24
33{
34#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
35 using std_thread_view = detail::thread_view_backend;
37
38 template <typename Function>
39 decltype(auto)
40 with_impl(Function&& function)
41 {
42 return std::visit(std::forward<Function>(function), impl_);
43 }
44
45 template <typename Function>
46 decltype(auto)
47 with_impl(Function&& function) const
48 {
49 return std::visit(std::forward<Function>(function), impl_);
50 }
51
52 std::variant<std_thread_view, jthread_view> impl_;
53#else
54 template <typename Function>
55 decltype(auto)
56 with_impl(Function&& function)
57 {
58 return std::forward<Function>(function)(impl_);
59 }
60
61 template <typename Function>
62 decltype(auto)
63 with_impl(Function&& function) const
64 {
65 return std::forward<Function>(function)(impl_);
66 }
67
69#endif
70
71public:
72#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
79 explicit thread_view(std::thread& value THREADSCHEDULE_LIFETIMEBOUND) noexcept
80 : impl_(std::in_place_type<std_thread_view>, value)
81 {
82 }
84 explicit thread_view(thread& value THREADSCHEDULE_LIFETIMEBOUND) noexcept
85 : impl_(std::in_place_type<std_thread_view>, value.impl_.get(), value.impl_.native_id())
86 {
87 }
94 explicit thread_view(std::jthread& value THREADSCHEDULE_LIFETIMEBOUND) noexcept
95 : impl_(std::in_place_type<jthread_view>, value)
96 {
97 }
99 explicit thread_view(jthread& value THREADSCHEDULE_LIFETIMEBOUND) noexcept
100 : impl_(std::in_place_type<jthread_view>, value.impl_, value.native_id_)
101 {
102 }
103#else
110 explicit thread_view(std::thread& value THREADSCHEDULE_LIFETIMEBOUND) noexcept : impl_(value) {}
113 : impl_(value.impl_.get(), value.impl_.native_id())
114 {
115 }
116#endif
117
119 [[nodiscard]] auto
120 joinable() const noexcept -> bool
121 {
122 return with_impl([](auto const& value) { return value.joinable(); });
123 }
124
126 [[nodiscard]] auto
127 get_id() const noexcept -> std::thread::id
128 {
129 return with_impl([](auto const& value) { return value.get_id(); });
130 }
131
133 auto
135 {
136 return with_impl([&](auto& value) { return detail::portable_thread_control::configure(value, config); });
137 }
138
140 auto
142 {
143 return with_impl([&](auto& value) { return detail::portable_thread_control::set_priority(value, level); });
144 }
145
147 auto
149 {
150 return with_impl([&](auto& impl) { return detail::portable_thread_control::set_nice(impl, value); });
151 }
152
154 [[nodiscard]] auto
156 {
157 return with_impl([](auto const& value)
158 { return detail::portable_thread_control::get_priority(value.get_nice_value()); });
159 }
160
162 [[nodiscard]] auto
164 {
165 return with_impl([](auto const& value)
166 { return detail::portable_thread_control::get_nice(value.get_nice_value()); });
167 }
168
170 auto
172 {
173 return with_impl([&](auto& value) { return value.set_name(name); });
174 }
175
177 [[nodiscard]] auto
178 get_name() const -> result<std::string>
179 {
180 return with_impl([](auto const& value) { return value.get_name(); });
181 }
182
184 auto
186 {
187 return with_impl([&](auto& value) { return detail::portable_thread_control::set_affinity(value, affinity); });
188 }
189
191 [[nodiscard]] auto
193 {
194 return with_impl([](auto const& value)
195 { return detail::portable_thread_control::get_affinity(value.get_affinity()); });
196 }
197};
198
199} // namespace threadschedule
Polymorphic base providing common thread management operations.
Non-owning view over an externally managed std::thread.
Strongly-typed nice value in the POSIX range $[-20, 19]$.
Portable thread configuration bundle.
auto set_priority(priority_level level) -> result< void >
Set portable priority preset on viewed thread.
auto joinable() const noexcept -> bool
Return whether viewed thread is joinable.
auto get_priority() const -> result< priority_level >
Query portable priority preset of viewed thread.
thread_view(std::thread &value THREADSCHEDULE_LIFETIMEBOUND) noexcept
Create a view over an existing std::thread.
auto set_nice(nice_value value) -> result< void >
Set explicit nice value on viewed thread.
auto get_id() const noexcept -> std::thread::id
Return std::thread id of viewed thread.
auto get_name() const -> result< std::string >
Query viewed thread name.
auto configure(thread_config const &config THREADSCHEDULE_NOESCAPE) -> result< void >
Apply full portable configuration to viewed thread.
auto get_affinity() const -> result< thread_affinity >
Query viewed thread CPU affinity.
auto get_nice() const -> result< nice_value >
Query nice value of viewed thread.
thread_view(thread &value THREADSCHEDULE_LIFETIMEBOUND) noexcept
Create a view over a threadschedule::thread.
auto set_affinity(thread_affinity const &affinity THREADSCHEDULE_NOESCAPE) -> result< void >
Set viewed thread CPU affinity.
auto set_name(std::string const &name THREADSCHEDULE_NOESCAPE) -> result< void >
Set viewed thread name.
Owning thread wrapper with result-based lifecycle/configuration API.
Definition thread.hpp:29
Owning std::jthread wrapper with portable configuration helpers.
Optional compiler diagnostics for borrowed references and pointers.
#define THREADSCHEDULE_LIFETIMEBOUND
Definition lifetime.hpp:28
#define THREADSCHEDULE_POINTER
Definition lifetime.hpp:32
#define THREADSCHEDULE_NOESCAPE
Definition lifetime.hpp:38
auto set_nice(Control &control, nice_value value) -> result< void >
Definition control.hpp:205
auto get_affinity(result< native_thread_affinity > affinity) -> result< thread_affinity >
Definition control.hpp:234
auto set_priority(Control &control, priority_level level) -> result< void >
Definition control.hpp:198
auto get_priority(result< int > value) -> result< priority_level >
Definition control.hpp:211
auto get_nice(result< int > value) -> result< nice_value >
Definition control.hpp:219
auto configure(Control &control, thread_config const &config) -> result< void >
Definition control.hpp:191
auto set_affinity(Control &control, thread_affinity const &affinity) -> result< void >
Definition control.hpp:228
priority_level
Portable non-realtime priority levels.
expected< T, std::error_code > result
Standard result type used by public APIs.
Definition result.hpp:22
Owning std::thread wrapper with portable configuration helpers.
C++17 thread wrappers and non-owning views.
Portable thread startup and runtime configuration.