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

RAII wrapper around POSIX threads with a modern C++ interface. More...

#include <pthread_wrapper.hpp>

Inheritance diagram for threadschedule::PThreadWrapper:
[legend]

Public Types

using native_handle_type = pthread_t
using id = pthread_t

Public Member Functions

template<typename F, typename... Args>
 PThreadWrapper (F &&func, Args &&... args)
 PThreadWrapper (PThreadWrapper const &)=delete
auto operator= (PThreadWrapper const &) -> PThreadWrapper &=delete
 PThreadWrapper (PThreadWrapper &&other) noexcept
auto operator= (PThreadWrapper &&other) noexcept -> PThreadWrapper &
void join ()
void detach ()
auto joinable () const -> bool
auto get_id () const -> id
auto native_handle () const -> native_handle_type
auto set_name (std::string const &name) const -> expected< void, std::error_code >
auto get_name () const -> std::optional< std::string >
auto set_priority (ThreadPriority priority) const -> expected< void, std::error_code >
auto set_scheduling_policy (SchedulingPolicy policy, ThreadPriority priority) const -> expected< void, std::error_code >
auto set_affinity (ThreadAffinity const &affinity) const -> expected< void, std::error_code >
auto get_affinity () const -> std::optional< ThreadAffinity >
auto cancel () const -> expected< void, std::error_code >

Static Public Member Functions

static auto set_cancel_state (bool enabled) -> expected< void, std::error_code >
static auto set_cancel_type (bool asynchronous) -> expected< void, std::error_code >
template<typename F, typename... Args>
static auto create_with_config (std::string const &name, SchedulingPolicy policy, ThreadPriority priority, F &&f, Args &&... args) -> PThreadWrapper
template<typename F, typename... Args>
static auto create_with_attributes (pthread_attr_t const &attr, F &&func, Args &&... args) -> PThreadWrapper

Detailed Description

RAII wrapper around POSIX threads with a modern C++ interface.

Linux-only – not available on Windows (guarded by _WIN32).

Non-copyable, movable. The destructor automatically joins the thread if it is still joinable, which blocks until the thread finishes.

Internally stores the callable in a heap-allocated std::function so that it can be passed through the C pthread_create API.

Note
Thread names are limited to 15 characters on Linux (enforced by pthread_setname_np).
cancel() sends a POSIX cancellation request to the thread. set_cancel_state() and set_cancel_type() are static and affect the calling thread, not the PThreadWrapper's thread.
Factory methods
  • create_with_config() – creates a thread and applies name/policy/priority.
  • create_with_attributes() – creates a thread from a raw pthread_attr_t.
See also
is_thread_like<PThreadWrapper> (specialised to true_type at end of file)

Definition at line 48 of file pthread_wrapper.hpp.

Member Typedef Documentation

◆ id

using threadschedule::PThreadWrapper::id = pthread_t

Definition at line 52 of file pthread_wrapper.hpp.

◆ native_handle_type

using threadschedule::PThreadWrapper::native_handle_type = pthread_t

Definition at line 51 of file pthread_wrapper.hpp.

Constructor & Destructor Documentation

◆ PThreadWrapper() [1/3]

threadschedule::PThreadWrapper::PThreadWrapper ( )
inline

Definition at line 54 of file pthread_wrapper.hpp.

◆ PThreadWrapper() [2/3]

template<typename F, typename... Args>
threadschedule::PThreadWrapper::PThreadWrapper ( F && func,
Args &&... args )
inlineexplicit

Definition at line 59 of file pthread_wrapper.hpp.

◆ PThreadWrapper() [3/3]

threadschedule::PThreadWrapper::PThreadWrapper ( PThreadWrapper && other)
inlinenoexcept

Definition at line 79 of file pthread_wrapper.hpp.

◆ ~PThreadWrapper()

threadschedule::PThreadWrapper::~PThreadWrapper ( )
inline

Definition at line 101 of file pthread_wrapper.hpp.

Member Function Documentation

◆ cancel()

auto threadschedule::PThreadWrapper::cancel ( ) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 218 of file pthread_wrapper.hpp.

◆ create_with_attributes()

template<typename F, typename... Args>
auto threadschedule::PThreadWrapper::create_with_attributes ( pthread_attr_t const & attr,
F && func,
Args &&... args ) -> PThreadWrapper
inlinestatic

Definition at line 256 of file pthread_wrapper.hpp.

◆ create_with_config()

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

Definition at line 245 of file pthread_wrapper.hpp.

◆ detach()

void threadschedule::PThreadWrapper::detach ( )
inline

Definition at line 123 of file pthread_wrapper.hpp.

◆ get_affinity()

auto threadschedule::PThreadWrapper::get_affinity ( ) const -> std::optional<ThreadAffinity>
inlinenodiscard

Definition at line 207 of file pthread_wrapper.hpp.

◆ get_id()

auto threadschedule::PThreadWrapper::get_id ( ) const -> id
inlinenodiscard

Definition at line 140 of file pthread_wrapper.hpp.

◆ get_name()

auto threadschedule::PThreadWrapper::get_name ( ) const -> std::optional<std::string>
inlinenodiscard

Definition at line 159 of file pthread_wrapper.hpp.

◆ join()

void threadschedule::PThreadWrapper::join ( )
inline

Definition at line 110 of file pthread_wrapper.hpp.

◆ joinable()

auto threadschedule::PThreadWrapper::joinable ( ) const -> bool
inlinenodiscard

Definition at line 135 of file pthread_wrapper.hpp.

◆ native_handle()

auto threadschedule::PThreadWrapper::native_handle ( ) const -> native_handle_type
inlinenodiscard

Definition at line 144 of file pthread_wrapper.hpp.

◆ operator=()

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

Definition at line 85 of file pthread_wrapper.hpp.

◆ set_affinity()

auto threadschedule::PThreadWrapper::set_affinity ( ThreadAffinity const & affinity) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 200 of file pthread_wrapper.hpp.

◆ set_cancel_state()

auto threadschedule::PThreadWrapper::set_cancel_state ( bool enabled) -> expected<void, std::error_code>
inlinestatic

Definition at line 225 of file pthread_wrapper.hpp.

◆ set_cancel_type()

auto threadschedule::PThreadWrapper::set_cancel_type ( bool asynchronous) -> expected<void, std::error_code>
inlinestatic

Definition at line 234 of file pthread_wrapper.hpp.

◆ set_name()

auto threadschedule::PThreadWrapper::set_name ( std::string const & name) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 150 of file pthread_wrapper.hpp.

◆ set_priority()

auto threadschedule::PThreadWrapper::set_priority ( ThreadPriority priority) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 169 of file pthread_wrapper.hpp.

◆ set_scheduling_policy()

auto threadschedule::PThreadWrapper::set_scheduling_policy ( SchedulingPolicy policy,
ThreadPriority priority ) const -> expected<void, std::error_code>
inlinenodiscard

Definition at line 184 of file pthread_wrapper.hpp.


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