NUMA++ 0.11.0
Loading...
Searching...
No Matches
forEachExample.cpp
1#include <chrono>
2#include <iostream>
3
4#include <numapp/cpumask.hpp>
5#include <numapp/numa.hpp>
6#include <numapp/thread.hpp>
7
8void ThreadFunc(std::chrono::milliseconds duration) {
9 std::cout << "Thread affinity: " << numapp::CpuAffinity::MakeFromActive() << std::endl;
10
11 std::this_thread::sleep_for(duration);
12}
13
14int main() {
15 using namespace numapp;
16 using namespace std::chrono_literals;
17 if (!NumaAvailable()) {
18 std::cout << "NUMA not available";
19 return -1;
20 }
21
22 std::vector<std::thread> threads;
23 std::string name = "thread-";
24 // Launch thread for each CPU in CPU string
25 ForEach(Cpumask::MakeFromCpuStringAll("0-3"), [&](unsigned int cpu) {
26 threads.push_back(MakeThread(
27 name + std::to_string(cpu), CpuAffinity::MakeBindCpu(cpu), &ThreadFunc, 500ms));
28 });
29
30 for (auto& thread : threads) {
31 thread.join();
32 }
33}
static CpuAffinity MakeBindCpu(int cpu)
Create CpuAffinity bound to the specified CPU.
static CpuAffinity MakeFromActive()
Create current affinity settings.
static Cpumask MakeFromCpuStringAll(char const *cpustring)
Construct Cpumask from cpustring that does not consider current cpuset.
Definition cpumask.hpp:44
Contains declarations for CpumaskTrait.
bool NumaAvailable() noexcept
Query whether system has NUMA support.
Definition numa.hpp:34
void ForEach(Bitmask< Class > const &bitmask, F &&f, bool value=true)
Invoke function for each bit in mask that matches value.
Definition bitmask.hpp:220
std::thread MakeThread(std::string_view thread_name, NumaPolicies const &policies, Func &&func, Args &&... args)
Primary overload accepting string-view for thread_name.
Definition thread.hpp:144
Contains declarations for numapp thread utilities.