NUMA++ 0.12.0
Loading...
Searching...
No Matches
forEachExample.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @copyright
4 * SPDX-FileCopyrightText: 2023-2023 European Southern Observatory (ESO)
5 *
6 * SPDX-License-Identifier: LGPL-3.0-only
7 */
8#include <chrono>
9#include <iostream>
10
11#include <numapp/cpumask.hpp>
12#include <numapp/numa.hpp>
13#include <numapp/thread.hpp>
14
15void ThreadFunc(std::chrono::milliseconds duration) {
16 std::cout << "Thread affinity: " << numapp::CpuAffinity::MakeFromActive() << std::endl;
17
18 std::this_thread::sleep_for(duration);
19}
20
21int main() {
22 using namespace numapp;
23 using namespace std::chrono_literals;
24 if (!NumaAvailable()) {
25 std::cout << "NUMA not available";
26 return -1;
27 }
28
29 std::vector<std::thread> threads;
30 std::string name = "thread-";
31 // Launch thread for each CPU in CPU string
32 ForEach(Cpumask::MakeFromCpuStringAll("0-3"), [&](unsigned int cpu) {
33 threads.push_back(MakeThread(
34 name + std::to_string(cpu), CpuAffinity::MakeBindCpu(cpu), &ThreadFunc, 500ms));
35 });
36
37 for (auto& thread : threads) {
38 thread.join();
39 }
40}
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:46
Contains declarations for CpumaskTrait.
bool NumaAvailable() noexcept
Query whether system has NUMA support.
Definition numa.hpp:35
void ForEach(Bitmask< Class > const &bitmask, F &&f, bool value=true)
Invoke function for each bit in mask that matches value.
Definition bitmask.hpp:222
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:146
Contains declarations for numapp thread utilities.