ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
thread_view.hpp
Go to the documentation of this file.
1#pragma once
2
10#include "jthread.hpp"
11#include "thread.hpp"
12#include "thread_config.hpp"
13
14#include <string>
15#include <thread>
16#include <utility>
17#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
18# include <variant>
19#endif
20
21namespace threadschedule
22{
23
30{
31#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
32 using std_thread_view = detail::thread_view_backend;
34
35 template <typename Function>
36 decltype(auto)
37 with_impl(Function&& function)
38 {
39 return std::visit(std::forward<Function>(function), impl_);
40 }
41
42 template <typename Function>
43 decltype(auto)
44 with_impl(Function&& function) const
45 {
46 return std::visit(std::forward<Function>(function), impl_);
47 }
48
49 std::variant<std_thread_view, jthread_view> impl_;
50#else
51 template <typename Function>
52 decltype(auto)
53 with_impl(Function&& function)
54 {
55 return std::forward<Function>(function)(impl_);
56 }
57
58 template <typename Function>
59 decltype(auto)
60 with_impl(Function&& function) const
61 {
62 return std::forward<Function>(function)(impl_);
63 }
64
66#endif
67
68public:
69#if defined(__cpp_lib_jthread) && __cpp_lib_jthread >= 201911L
76 explicit thread_view(std::thread& value) noexcept : impl_(std::in_place_type<std_thread_view>, value) {}
78 explicit thread_view(thread& value) noexcept
79 : impl_(std::in_place_type<std_thread_view>, value.impl_.get(), value.impl_.native_id())
80 {
81 }
88 explicit thread_view(std::jthread& value) noexcept : impl_(std::in_place_type<jthread_view>, value) {}
90 explicit thread_view(jthread& value) noexcept : impl_(std::in_place_type<jthread_view>, value.impl_, value.native_id_)
91 {
92 }
93#else
100 explicit thread_view(std::thread& value) noexcept : impl_(value) {}
102 explicit thread_view(thread& value) noexcept : impl_(value.impl_.get(), value.impl_.native_id()) {}
103#endif
104
106 [[nodiscard]] auto
107 joinable() const noexcept -> bool
108 {
109 return with_impl([](auto const& value) { return value.joinable(); });
110 }
111
113 [[nodiscard]] auto
114 get_id() const noexcept -> std::thread::id
115 {
116 return with_impl([](auto const& value) { return value.get_id(); });
117 }
118
120 auto
122 {
123 return with_impl([&](auto& value) { return detail::portable_thread_control::configure(value, config); });
124 }
125
127 auto
129 {
130 return with_impl([&](auto& value) { return detail::portable_thread_control::set_priority(value, level); });
131 }
132
134 auto
136 {
137 return with_impl([&](auto& impl) { return detail::portable_thread_control::set_nice(impl, value); });
138 }
139
141 [[nodiscard]] auto
143 {
144 return with_impl([](auto const& value)
145 { return detail::portable_thread_control::get_priority(value.get_nice_value()); });
146 }
147
149 [[nodiscard]] auto
151 {
152 return with_impl([](auto const& value)
153 { return detail::portable_thread_control::get_nice(value.get_nice_value()); });
154 }
155
157 auto
158 set_name(std::string const& name) -> result<void>
159 {
160 return with_impl([&](auto& value) { return value.set_name(name); });
161 }
162
164 [[nodiscard]] auto
165 get_name() const -> result<std::string>
166 {
167 return with_impl([](auto const& value) { return value.get_name(); });
168 }
169
171 auto
173 {
174 return with_impl([&](auto& value) { return detail::portable_thread_control::set_affinity(value, affinity); });
175 }
176
178 [[nodiscard]] auto
180 {
181 return with_impl([](auto const& value)
182 { return detail::portable_thread_control::get_affinity(value.get_affinity()); });
183 }
184};
185
186} // 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.
Non-owning view over a running thread or jthread.
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.
auto set_nice(nice_value value) -> result< void >
Set explicit nice value on viewed thread.
auto set_name(std::string const &name) -> result< void >
Set viewed thread name.
thread_view(std::thread &value) noexcept
Create a view over an existing std::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 set_affinity(thread_affinity const &affinity) -> result< void >
Set viewed thread CPU affinity.
thread_view(thread &value) noexcept
Create a view over a threadschedule::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.
auto configure(thread_config const &config) -> result< void >
Apply full portable configuration to viewed thread.
Owning thread wrapper with result-based lifecycle/configuration API.
Definition thread.hpp:29
Owning std::jthread wrapper with portable configuration helpers.
priority_level
Portable non-realtime priority levels.
Owning std::thread wrapper with portable configuration helpers.
C++17 thread wrappers and non-owning views.
Portable thread startup and runtime configuration.