ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
threadschedule::detail::native_thread_affinity Class Reference

Manages a set of CPU indices to which a thread may be bound. More...

#include <native.hpp>

Public Member Functions

 native_thread_affinity ()
 
 native_thread_affinity (std::vector< int > const &cpus)
 
void add_cpu (int cpu)
 
void remove_cpu (int cpu)
 
auto is_set (int cpu) const -> bool
 
auto has_cpu (int cpu) const -> bool
 
void clear ()
 
auto get_cpus () const -> std::vector< int >
 
auto native_handle () const -> cpu_set_t const &
 
auto to_string () const -> std::string
 

Detailed Description

Manages a set of CPU indices to which a thread may be bound.

native_thread_affinity is a value-semantic type that represents a CPU affinity mask. It abstracts away the platform-specific details of cpu_set_t (Linux) and processor-group bitmasks (Windows).

Linux

Backed by a cpu_set_t. Supports CPU indices in the range [0, CPU_SETSIZE) (typically 0-1023). The native_handle() accessor provides a const cpu_set_t& for direct use with pthread_setaffinity_np or sched_setaffinity.

Windows

Backed by a 64-bit bitmask plus a processor group index (WORD). Windows organises logical processors into groups of up to 64. This class supports a single group at a time: the group is determined by the first CPU added via add_cpu(). Subsequent calls to add_cpu() for CPUs that belong to a different group are silently ignored. Use get_group() and get_mask() to retrieve the platform-native values for SetThreadGroupAffinity.

Thread safety

None. native_thread_affinity is a plain value type with no internal synchronisation. Concurrent reads are safe; concurrent mutation (or a read concurrent with a write) requires external locking.

Copyability / movability

Implicitly copyable and movable (compiler-generated special members).

Warning
On Windows, CPUs from different processor groups cannot be combined in a single native_thread_affinity instance. If you need cross-group affinity you must apply separate native_thread_affinity objects per group.

Definition at line 432 of file native.hpp.

Constructor & Destructor Documentation

◆ native_thread_affinity() [1/2]

threadschedule::detail::native_thread_affinity::native_thread_affinity ( )
inline

Definition at line 435 of file native.hpp.

◆ native_thread_affinity() [2/2]

threadschedule::detail::native_thread_affinity::native_thread_affinity ( std::vector< int > const &  cpus)
inlineexplicit

Definition at line 445 of file native.hpp.

References add_cpu().

Member Function Documentation

◆ add_cpu()

void threadschedule::detail::native_thread_affinity::add_cpu ( int  cpu)
inline

Definition at line 456 of file native.hpp.

Referenced by native_thread_affinity().

◆ clear()

void threadschedule::detail::native_thread_affinity::clear ( )
inline

Definition at line 522 of file native.hpp.

◆ get_cpus()

auto threadschedule::detail::native_thread_affinity::get_cpus ( ) const -> std::vector<int>
inline

Definition at line 532 of file native.hpp.

Referenced by to_string().

◆ has_cpu()

auto threadschedule::detail::native_thread_affinity::has_cpu ( int  cpu) const -> bool
inline

Definition at line 516 of file native.hpp.

References is_set().

◆ is_set()

auto threadschedule::detail::native_thread_affinity::is_set ( int  cpu) const -> bool
inline

Definition at line 502 of file native.hpp.

Referenced by has_cpu().

◆ native_handle()

auto threadschedule::detail::native_thread_affinity::native_handle ( ) const -> cpu_set_t const&
inline

Definition at line 573 of file native.hpp.

◆ remove_cpu()

void threadschedule::detail::native_thread_affinity::remove_cpu ( int  cpu)
inline

Definition at line 482 of file native.hpp.

◆ to_string()

auto threadschedule::detail::native_thread_affinity::to_string ( ) const -> std::string
inline

Definition at line 580 of file native.hpp.

References get_cpus().


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