ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
thread_affinity.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "cpu_id.hpp"
9
10#include <algorithm>
11#include <initializer_list>
12#include <utility>
13#include <vector>
14
15namespace threadschedule
16{
17
19{
20public:
22 thread_affinity() = default;
27 explicit thread_affinity(std::vector<cpu_id> cpus) : cpus_(std::move(cpus))
28 {
29 normalize();
30 }
31
36 thread_affinity(std::initializer_list<cpu_id> cpus) : cpus_(cpus)
37 {
38 normalize();
39 }
40
45 void
47 {
48 if (contains(cpu))
49 return;
50 cpus_.push_back(cpu);
51 std::sort(cpus_.begin(), cpus_.end());
52 }
53
58 void
60 {
61 cpus_.erase(std::remove(cpus_.begin(), cpus_.end(), cpu), cpus_.end());
62 }
63
65 void
66 clear() noexcept
67 {
68 cpus_.clear();
69 }
70
75 [[nodiscard]] auto
76 contains(cpu_id cpu) const noexcept -> bool
77 {
78 return std::binary_search(cpus_.begin(), cpus_.end(), cpu);
79 }
80
82 [[nodiscard]] auto
83 empty() const noexcept -> bool
84 {
85 return cpus_.empty();
86 }
87
89 [[nodiscard]] auto
90 cpus() const noexcept -> std::vector<cpu_id> const&
91 {
92 return cpus_;
93 }
94
95private:
96 void
97 normalize()
98 {
99 std::sort(cpus_.begin(), cpus_.end());
100 cpus_.erase(std::unique(cpus_.begin(), cpus_.end()), cpus_.end());
101 }
102
103 std::vector<cpu_id> cpus_;
104};
105
106} // namespace threadschedule
Validated CPU identifier.
Definition cpu_id.hpp:23
void remove_cpu(cpu_id cpu)
Remove a CPU from the affinity set.
thread_affinity(std::vector< cpu_id > cpus)
Construct from a list of CPUs.
auto cpus() const noexcept -> std::vector< cpu_id > const &
Return the normalized sorted list of CPUs in the set.
thread_affinity(std::initializer_list< cpu_id > cpus)
Construct from an initializer list of CPUs.
thread_affinity()=default
Construct an empty affinity set (no CPU pinning requested).
auto contains(cpu_id cpu) const noexcept -> bool
Check whether a CPU is present in the affinity set.
void clear() noexcept
Remove all CPUs from the affinity set.
void add_cpu(cpu_id cpu)
Add a CPU to the affinity set.
auto empty() const noexcept -> bool
Return whether the affinity set is empty.
Strongly-typed CPU index used by affinity-related APIs.