39 explicit thread(std::thread&& value) noexcept : impl_(std::move(value)) {}
45 template <
typename F,
typename... Args,
47 !std::is_same_v<std::decay_t<F>,
thread> && !std::is_same_v<std::decay_t<F>,
thread_config>,
int> = 0>
48 explicit thread(F&& function, Args&&... args) : impl_(
std::forward<F>(function),
std::forward<Args>(args)...)
58 template <
typename F,
typename... Args>
60 : impl_(make_configured_impl(config,
std::forward<F>(function),
std::forward<Args>(args)...))
74 template <typename F, typename... Args>
80 {
return thread(std::forward<F>(function), std::forward<Args>(args)...); });
89 template <
typename F,
typename... Args>
94 {
return thread(config, std::forward<F>(function), std::forward<Args>(args)...); });
101 return detail::thread_lifecycle::join(impl_);
108 detail::thread_lifecycle::join_or_throw(impl_,
"thread::join");
115 return detail::thread_lifecycle::detach(impl_);
122 detail::thread_lifecycle::detach_or_throw(impl_,
"thread::detach");
140 [[nodiscard]]
static auto
143 return std::thread::hardware_concurrency();
150 return detail::portable_thread_control::configure(impl_, config);
157 return detail::portable_thread_control::set_priority(impl_, level);
164 return detail::portable_thread_control::set_nice(impl_, value);
171 return detail::portable_thread_control::get_priority(impl_.
get_nice_value());
178 return detail::portable_thread_control::get_nice(impl_.
get_nice_value());
199 return detail::portable_thread_control::set_affinity(impl_, affinity);
206 return detail::portable_thread_control::get_affinity(impl_.
get_affinity());
220 template <
typename F,
typename... Args>
224 auto gate = std::make_shared<detail::thread_start_gate>();
226 [gate, callable =
detail::bind_args(std::forward<F>(function), std::forward<Args>(args)...)]()
mutable
235 gate->release(
false);
238 if (value.joinable())
248 throw std::system_error(configured.error(),
"thread configuration");
255 detail::thread_backend impl_;
C++17 callable and argument binding with move-only support.
auto get_id() const noexcept -> id
auto set_name(std::string const &name) -> expected< void, std::error_code >
auto get_affinity() const -> expected< native_thread_affinity, std::error_code >
auto get_nice_value() const -> expected< int, std::error_code >
auto joinable() const noexcept -> bool
auto configure(native_scheduling_config const &config) -> expected< void, std::error_code >
auto get_name() const -> expected< std::string, std::error_code >
Owning wrapper around std::thread with RAII join-on-destroy semantics.
auto release() noexcept -> 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.
Owning thread wrapper with result-based lifecycle/configuration API.
auto detach() -> result< void >
Detach the thread.
auto release() noexcept -> std::thread
Release ownership and return underlying std::thread.
static auto create(thread_config const &config, F &&function, Args &&... args) -> result< thread >
Create configured thread without throwing.
static auto create(F &&function, Args &&... args) -> std::enable_if_t<!std::is_same_v< std::decay_t< F >, thread_config >, result< thread > >
Create thread without throwing.
thread(std::thread &&value) noexcept
Take ownership of an existing std::thread.
auto set_name(std::string const &name) -> result< void >
Set thread name.
thread(thread_config const &config, F &&function, Args &&... args)
Start a thread and apply thread_config before user code runs.
auto set_nice(nice_value value) -> result< void >
Set explicit nice value.
static auto hardware_concurrency() noexcept -> unsigned
Forward std::thread::hardware_concurrency().
auto get_nice() const -> result< nice_value >
Query current nice value.
void detach_or_throw()
Throwing counterpart to detach.
thread(thread &&) noexcept=default
thread(F &&function, Args &&... args)
Start a thread from callable and arguments.
auto set_priority(priority_level level) -> result< void >
Set portable priority preset.
thread()=default
Construct an empty, non-joinable thread object.
void join_or_throw()
Throwing counterpart to join.
auto configure(thread_config const &config) -> result< void >
Apply a full portable thread configuration to the running thread.
auto get_priority() const -> result< priority_level >
Query current portable priority preset.
auto get_name() const -> result< std::string >
Query thread name if supported by platform/backend.
auto get_id() const noexcept -> std::thread::id
Return std::thread id of the owned thread.
auto join() -> result< void >
Join the thread.
auto set_affinity(thread_affinity const &affinity) -> result< void >
Apply CPU affinity set.
auto get_affinity() const -> result< thread_affinity >
Query current CPU affinity.
auto joinable() const noexcept -> bool
Return whether the thread object owns a running thread.
auto make_scope_exit(F &&function) -> scope_exit< std::decay_t< F > >
constexpr auto to_native(shutdown_policy policy) noexcept -> shutdown_policy_backend
auto bind_args(F &&function, Args &&... args)
auto try_result(Function &&function) -> decltype(std::forward< Function >(function)())
priority_level
Portable non-realtime priority levels.
C++17 thread wrappers and non-owning views.
Portable thread startup and runtime configuration.