33 lines
902 B
C++
33 lines
902 B
C++
#include <iostream>
|
|
#include <thread>
|
|
#if PLATFORM_WINDOWS
|
|
#include <windows.h>
|
|
#include <processthreadsapi.h>
|
|
#elif PLATFORM_LINUX
|
|
#include <pthread.h>
|
|
#elif PLATFORM_MACOS
|
|
#include <pthread.h>
|
|
#endif
|
|
|
|
inline void set_thread_name(const char* name) {
|
|
#if PLATFORM_WINDOWS
|
|
SetThreadDescription(GetCurrentThread(), std::wstring(name, name + strlen(name)).c_str());
|
|
#elif PLATFORM_LINUX
|
|
pthread_setname_np(pthread_self(), name);
|
|
#elif PLATFORM_MACOS
|
|
pthread_setname_np(name);
|
|
#endif
|
|
}
|
|
|
|
inline void set_thread_affinity(std::thread& thread, int core) {
|
|
#if PLATFORM_WINDOWS
|
|
const auto handle = reinterpret_cast<HANDLE>(thread.native_handle());
|
|
SetThreadAffinityMask(handle, 1 << core);
|
|
#elif PLATFORM_LINUX || PLATFORM_MACOS
|
|
cpu_set_t cpuset;
|
|
CPU_ZERO(&cpuset);
|
|
CPU_SET(core, &cpuset);
|
|
pthread_setaffinity_np(thread.native_handle(), sizeof(cpu_set_t), &cpuset);
|
|
#endif
|
|
}
|