|
NUMA++ 0.11.0
|
NUMA++ thread APIs. More...
Files | |
| file | thread.cpp |
| Definition of thread functions. | |
Functions | |
| pid_t | numapp::thisThread::GetThreadId () noexcept |
| Query the thread id "TID" of the current thread. | |
| void | numapp::thisThread::SetThreadName (std::string_view thread_name, std::error_code &ec) noexcept |
| Set thread name for current thread. | |
| void | numapp::thisThread::SetThreadName (std::string_view thread_name) |
| Set thread name for current thread (throwing version). | |
| std::string | numapp::thisThread::GetThreadName (std::error_code &ec) noexcept |
| Get name of current thread. | |
| std::string | numapp::thisThread::GetThreadName () |
| Get name of current thread (throwing version). | |
Makes a std::thread or std::jthread with provided NUMA policies | |||||||||||||||
Create a named thread with optional CPU affinity, scheduler and memory policies. Function has the following effects in this thread
Minimum application example with a #include <chrono>
#include <iostream>
#include <numapp/numa.hpp>
#include <numapp/thread.hpp>
void ThreadFunc(std::chrono::milliseconds duration) {
std::cout << "Thread affinity: " << numapp::CpuAffinity::MakeFromActive() << std::endl;
std::this_thread::sleep_for(duration);
}
int main() {
using namespace numapp;
using namespace std::chrono_literals;
if (!NumaAvailable()) {
std::cout << "NUMA not available";
return -1;
}
NumaPolicies policies;
thread.join();
}
static CpuAffinity MakeFromCpuStringAll(char const *cpustring) Create CpuAffinity from `cpustring` without considering current cpuset. Definition cpuaffinity.cpp:47 Combines the the available NUMA policy types in one object. Definition numapolicies.hpp:49 void SetCpuAffinity(std::optional< CpuAffinity > affinity) noexcept Set CPU affinity. Definition numapolicies.hpp:114 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 Definition bitmask.cpp:11 Contains declarations for numapp thread utilities. Example function that create pinned thread with local NUMA node memory policy: #include <numapp/memory.hpp>
#include <numapp/thread.hpp>
template <class F, class... Args>
auto MakePinnedThread(int cpu, std::string_view name, F&& f, Args... args) -> std::thread {
using namespace numapp;
NumaPolicies policies;
policies.SetCpuAffinity(CpuAffinity::MakeBindCpu(cpu));
auto node = GetNodeOfCpu(cpu);
if (!node.has_value()) {
throw std::invalid_argument("cpu invalid");
}
policies.SetMemPolicy(MemPolicy::MakeBindNode(*node));
return MakeThread(name, policies, std::forward<F>(f), std::forward<Args>(args)...);
}
static CpuAffinity MakeBindCpu(int cpu) Create CpuAffinity bound to the specified CPU. Definition cpuaffinity.cpp:59 static MemPolicy MakeBindNode(int node) Creates a strict policy (using MPOL_BIND) to allocate all memory to the specified node. void SetMemPolicy(std::optional< MemPolicy > policy) noexcept Set memory policy. Definition numapolicies.hpp:138 std::optional< int > GetNodeOfCpu(int cpu) noexcept Get NUMA node of the given CPU. Definition memory.cpp:53 Contains memory function declarations. Similar to first example, but uses member function instead: #include <chrono>
#include <iostream>
#include <numapp/numa.hpp>
#include <numapp/thread.hpp>
struct Example {
Example(std::chrono::milliseconds duration) : m_duration(duration) {
}
Example(Example&&) = default;
void ThreadFunc() {
std::cout << "Thread affinity: "
<< numapp::CpuAffinity::MakeFromActive() << std::endl;
std::this_thread::sleep_for(m_duration);
}
std::chrono::milliseconds m_duration;
};
int main() {
using namespace numapp;
using namespace std::chrono_literals;
if (!NumaAvailable()) {
std::cout << "NUMA not available";
return -1;
}
NumaPolicies policies;
auto thread =
MakeJthread("myThread", policies, &Example::ThreadFunc, Example{500ms});
}
| |||||||||||||||
| template<class Func, class... Args> | |||||||||||||||
| std::thread | numapp::MakeThread (std::string_view thread_name, NumaPolicies const &policies, Func &&func, Args &&... args) | ||||||||||||||
Primary overload accepting string-view for thread_name. | |||||||||||||||
| template<class Func, class... Args> | |||||||||||||||
| std::thread | numapp::MakeThread (char const *thread_name, NumaPolicies const &policies, Func &&func, Args &&... args) | ||||||||||||||
| Compatibility overload accepting null terminated C string for thread_name. | |||||||||||||||
NUMA++ thread APIs.
|
nodiscardnoexcept |
Query the thread id "TID" of the current thread.
It is provided in NUMA++ as gettid() is not provided by glibc.
Definition at line 27 of file thread.cpp.
|
nodiscard |
Get name of current thread (throwing version).
| std::bad_alloc | if allocation fails. |
Definition at line 75 of file thread.cpp.
|
nodiscardnoexcept |
Get name of current thread.
| [out] | ec | out-parameter for error reporting. |
| std::bad_alloc | if allocation fails. |
Definition at line 57 of file thread.cpp.
|
nodiscard |
Compatibility overload accepting null terminated C string for thread_name.
See MakeThread group for detailed documentation.
Definition at line 187 of file thread.hpp.
|
nodiscard |
Primary overload accepting string-view for thread_name.
See MakeThread group for detailed documentation.
Definition at line 144 of file thread.hpp.
| void numapp::thisThread::SetThreadName | ( | std::string_view | thread_name | ) |
Set thread name for current thread (throwing version).
| thread_name | Name of thread. Maximum length (thread_name.length()) is 15. |
| std::system_error | on errors. |
Definition at line 49 of file thread.cpp.
|
noexcept |
Set thread name for current thread.
| thread_name | Name of thread. Maximum length (thread_name.length()) is 15. | |
| [out] | ec | out-parameter for error reporting.
|
Definition at line 32 of file thread.cpp.