ThreadSchedule 2.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::ThreadWrapper Class Reference

Owning wrapper around std::thread with RAII join-on-destroy semantics. More...

#include <thread_wrapper.hpp>

Inheritance diagram for threadschedule::ThreadWrapper:
[legend]
Collaboration diagram for threadschedule::ThreadWrapper:
[legend]

Public Member Functions

 ThreadWrapper ()=default
 ThreadWrapper (std::thread &&t) noexcept
template<typename F, typename... Args>
 ThreadWrapper (F &&f, Args &&... args)
 ThreadWrapper (ThreadWrapper const &)=delete
auto operator= (ThreadWrapper const &) -> ThreadWrapper &=delete
 ThreadWrapper (ThreadWrapper &&other) noexcept
auto operator= (ThreadWrapper &&other) noexcept -> ThreadWrapper &
 ~ThreadWrapper () override
auto release () noexcept -> std::thread
 operator std::thread () &&noexcept
Public Member Functions inherited from threadschedule::BaseThreadWrapper< std::thread, detail::OwningTag >
 BaseThreadWrapper ()=default
virtual ~BaseThreadWrapper ()=default
void join ()
void detach ()
auto joinable () const noexcept -> bool
auto get_id () const noexcept -> id
auto native_handle () noexcept -> native_handle_type
auto set_name (std::string const &name) -> expected< void, std::error_code >
auto get_name () const -> std::optional< std::string >
auto set_priority (ThreadPriority priority) -> expected< void, std::error_code >
auto set_scheduling_policy (SchedulingPolicy policy, ThreadPriority priority) -> expected< void, std::error_code >
auto set_affinity (ThreadAffinity const &affinity) -> expected< void, std::error_code >
auto get_affinity () const -> std::optional< ThreadAffinity >

Static Public Member Functions

template<typename F, typename... Args>
static auto create_with_config (std::string const &name, SchedulingPolicy policy, ThreadPriority priority, F &&f, Args &&... args) -> ThreadWrapper
Static Public Member Functions inherited from threadschedule::BaseThreadWrapper< std::thread, detail::OwningTag >
static auto set_nice_value (int nice_value) -> bool
static auto get_nice_value () -> std::optional< int >

Additional Inherited Members

Public Types inherited from threadschedule::BaseThreadWrapper< std::thread, detail::OwningTag >
using native_handle_type
using id

Detailed Description

Owning wrapper around std::thread with RAII join-on-destroy semantics.

Extends BaseThreadWrapper to provide an owning, movable, non-copyable wrapper over std::thread. Adds automatic lifetime management: the destructor joins the thread if it is still joinable, which means destruction can block until the thread finishes.

Copyability / Movability
  • Not copyable (copy constructor and copy assignment are deleted).
  • Movable. Move construction transfers ownership. Move assignment first joins the currently held thread (blocking!) before taking ownership of the source thread.
Destruction
The destructor calls join() if the thread is joinable. This will block the destroying thread until the managed thread completes. If blocking destruction is undesirable, call detach() or release() before the wrapper goes out of scope.
release()
Transfers ownership of the underlying std::thread out of the wrapper, returning it by value. After release, the wrapper holds a default-constructed (non-joinable) thread and destruction becomes a no-op.
create_with_config()
Factory that creates a thread and attempts to set its name and scheduling policy. Failures from set_name() or set_scheduling_policy() are silently ignored - the thread will still be running but may not have the requested attributes. Check attributes after construction if they are critical.
Thread Safety
Not thread-safe. A single ThreadWrapper must not be mutated concurrently from multiple threads.

Definition at line 352 of file thread_wrapper.hpp.

Constructor & Destructor Documentation

◆ ThreadWrapper() [1/5]

threadschedule::ThreadWrapper::ThreadWrapper ( )
default

◆ ThreadWrapper() [2/5]

threadschedule::ThreadWrapper::ThreadWrapper ( std::thread && t)
inlinenoexcept

Definition at line 358 of file thread_wrapper.hpp.

◆ ThreadWrapper() [3/5]

template<typename F, typename... Args>
threadschedule::ThreadWrapper::ThreadWrapper ( F && f,
Args &&... args )
inlineexplicit

◆ ThreadWrapper() [4/5]

threadschedule::ThreadWrapper::ThreadWrapper ( ThreadWrapper const & )
delete

References ThreadWrapper().

◆ ThreadWrapper() [5/5]

threadschedule::ThreadWrapper::ThreadWrapper ( ThreadWrapper && other)
inlinenoexcept

Definition at line 372 of file thread_wrapper.hpp.

References ThreadWrapper().

◆ ~ThreadWrapper()

threadschedule::ThreadWrapper::~ThreadWrapper ( )
inlineoverride

Member Function Documentation

◆ create_with_config()

template<typename F, typename... Args>
auto threadschedule::ThreadWrapper::create_with_config ( std::string const & name,
SchedulingPolicy policy,
ThreadPriority priority,
F && f,
Args &&... args ) -> ThreadWrapper
inlinestatic

◆ operator std::thread()

threadschedule::ThreadWrapper::operator std::thread ( ) &&
inlineexplicitnoexcept

Definition at line 404 of file thread_wrapper.hpp.

Referenced by release().

◆ operator=() [1/2]

auto threadschedule::ThreadWrapper::operator= ( ThreadWrapper && other) -> ThreadWrapper&
inlinenoexcept

◆ operator=() [2/2]

auto threadschedule::ThreadWrapper::operator= ( ThreadWrapper const & ) -> ThreadWrapper &=delete
delete

References ThreadWrapper().

◆ release()

auto threadschedule::ThreadWrapper::release ( ) -> std::thread
inlinenoexcept

Definition at line 399 of file thread_wrapper.hpp.

References operator std::thread().


The documentation for this class was generated from the following file: