12#ifndef NUMAPP_THREAD_HPP_
13#define NUMAPP_THREAD_HPP_
18#include <system_error>
55void SetThreadName(std::string_view thread_name, std::error_code& ec) noexcept;
79[[nodiscard]] std::
string GetThreadName(std::error_code& ec) noexcept;
143template <class Func, class... Args>
144[[nodiscard]] std::thread
MakeThread(std::string_view thread_name,
148 static_assert(std::is_constructible_v<std::decay_t<Func>, Func>,
149 "ill formed - Func must be MoveConstructible");
150 static_assert((std::is_constructible_v<std::decay_t<Args>, Args> && ...),
151 "ill formed - Args must be MoveConstructible");
152 static_assert(std::is_invocable_v<std::decay_t<Func>, std::decay_t<Args>...>,
153 "ill formed - Func must be invocable with Args");
155 auto trampoline = detail::ThreadInitializer(thread_name, policies);
159 auto thr = std::thread(
160 [&trampoline, func = std::forward<Func>(func)](
auto&&... args)
mutable {
161 if (trampoline.Initialize() != std::error_code()) {
166 std::invoke(std::move(func), std::move(args)...);
168 std::forward<Args>(args)...);
170 auto result = trampoline.Wait();
174 throw std::system_error(result);
185template <
class Func,
class... Args>
186[[nodiscard]] std::thread
188 return MakeThread(std::string_view(thread_name),
190 std::forward<Func>(func),
191 std::forward<Args>(args)...);
194#ifdef __cpp_lib_jthread
209template <
class Func,
class... Args>
210[[nodiscard]] std::jthread MakeJthread(std::string_view thread_name,
211 NumaPolicies
const& policies,
214 static_assert(std::is_constructible_v<std::decay_t<Func>, Func>,
215 "ill formed - Func must be MoveConstructible");
216 static_assert((std::is_constructible_v<std::decay_t<Args>, Args> && ...),
217 "ill formed - Args must be MoveConstructible");
219 std::is_invocable_v<std::decay_t<Func>, std::decay_t<Args>...> ||
220 std::is_invocable_v<std::decay_t<Func>, std::stop_token, std::decay_t<Args>...>,
221 "ill formed - Func must be invocable with Args, with our without stop token");
223 auto trampoline = detail::ThreadInitializer(thread_name, policies);
226 auto thr = std::jthread(
227 [&trampoline, func = std::forward<Func>(func)](std::stop_token
const& token,
228 auto&&... args)
mutable {
229 if (trampoline.Initialize() != std::error_code()) {
234 if constexpr (std::is_invocable_v<std::decay_t<Func>,
236 std::decay_t<Args>...>) {
237 std::invoke(std::move(func), token, std::move(args)...);
239 std::invoke(std::move(func), std::move(args)...);
242 std::forward<Args>(args)...);
245 auto result = trampoline.Wait();
249 throw std::system_error(result);
Combines the the available NUMA policy types in one object.
pid_t GetThreadId() noexcept
Query the thread id "TID" of the current thread.
std::string GetThreadName(std::error_code &ec) noexcept
Get name of current thread.
std::thread MakeThread(std::string_view thread_name, NumaPolicies const &policies, Func &&func, Args &&... args)
Primary overload accepting string-view for thread_name.
void SetThreadName(std::string_view thread_name, std::error_code &ec) noexcept
Set thread name for current thread.
Contains declarations for NumaPolicies.