14#ifndef NUMAPP_THREAD_HPP_
15#define NUMAPP_THREAD_HPP_
20#include <system_error>
57void SetThreadName(std::string_view thread_name, std::error_code& ec) noexcept;
81[[nodiscard]] std::
string GetThreadName(std::error_code& ec) noexcept;
145template <class Func, class... Args>
146[[nodiscard]] std::thread
MakeThread(std::string_view thread_name,
150 static_assert(std::is_constructible_v<std::decay_t<Func>, Func>,
151 "ill formed - Func must be MoveConstructible");
152 static_assert((std::is_constructible_v<std::decay_t<Args>, Args> && ...),
153 "ill formed - Args must be MoveConstructible");
154 static_assert(std::is_invocable_v<std::decay_t<Func>, std::decay_t<Args>...>,
155 "ill formed - Func must be invocable with Args");
157 auto trampoline = detail::ThreadInitializer(thread_name, policies);
161 auto thr = std::thread(
162 [&trampoline, func = std::forward<Func>(func)](
auto&&... args)
mutable {
163 if (trampoline.Initialize() != std::error_code()) {
168 std::invoke(std::move(func), std::move(args)...);
170 std::forward<Args>(args)...);
172 auto result = trampoline.Wait();
176 throw std::system_error(result);
187template <
class Func,
class... Args>
188[[nodiscard]] std::thread
190 return MakeThread(std::string_view(thread_name),
192 std::forward<Func>(func),
193 std::forward<Args>(args)...);
196#ifdef __cpp_lib_jthread
211template <
class Func,
class... Args>
212[[nodiscard]] std::jthread MakeJthread(std::string_view thread_name,
213 NumaPolicies
const& policies,
216 static_assert(std::is_constructible_v<std::decay_t<Func>, Func>,
217 "ill formed - Func must be MoveConstructible");
218 static_assert((std::is_constructible_v<std::decay_t<Args>, Args> && ...),
219 "ill formed - Args must be MoveConstructible");
221 std::is_invocable_v<std::decay_t<Func>, std::decay_t<Args>...> ||
222 std::is_invocable_v<std::decay_t<Func>, std::stop_token, std::decay_t<Args>...>,
223 "ill formed - Func must be invocable with Args, with our without stop token");
225 auto trampoline = detail::ThreadInitializer(thread_name, policies);
228 auto thr = std::jthread(
229 [&trampoline, func = std::forward<Func>(func)](std::stop_token
const& token,
230 auto&&... args)
mutable {
231 if (trampoline.Initialize() != std::error_code()) {
236 if constexpr (std::is_invocable_v<std::decay_t<Func>,
238 std::decay_t<Args>...>) {
239 std::invoke(std::move(func), token, std::move(args)...);
241 std::invoke(std::move(func), std::move(args)...);
244 std::forward<Args>(args)...);
247 auto result = trampoline.Wait();
251 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.