Skip to content

C++ header-only utility for query or change thread affinity in a process

License

Notifications You must be signed in to change notification settings

fabiogaluppo/cpu_thread_affinity_helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CPU Thread Affinity Helper

C++ header-only utility for query or change thread affinity in a process

Compile instructions

  • Compile test driver for C++ with Visual C++
cl /O2 /EHsc /std:c++14 cpu_affinity_util.t.cpp
  • Compile test driver for C++ with g++ 10
g++-10 -O3 -std=c++14 -pthread cpu_affinity_util.t.cpp
  • Build a .dll for Windows with Visual C++
cl /O2 /EHsc /std:c++14 /DCPU_AFFINITY_UTIL_EXPORTS cpu_affinity_util_exports.cpp /link /DLL /out:cpu_affinity_util.dll
  • Compile test driver for Windows .dll with Visual C++
cl /O2 /EHsc /std:c++14 cpu_affinity_util.windows.dll.t.cpp
  • Build a .so for Linux with g++ 10
g++-10 -Wall -fPIC -c -O3 -std=c++14 -pthread cpu_affinity_util_exports.cpp 
g++-10 -shared -Wl,-soname,libcpu_affinity_util.so -o libcpu_affinity_util.so cpu_affinity_util_exports.o
  • Compile test driver for Linux .so with g++ 10
g++-10 -O3 -std=c++14 -pthread cpu_affinity_util.linux.so.t.cpp -ldl

Python example

  • Consuming Windows .dll in Python for query thread, process and processor info
from ctypes import *
dll = cdll.LoadLibrary("cpu_affinity_util.dll");
print("process id =", dll.cpuh_get_process_id())
print("thread id =", dll.cpuh_get_thread_id())
print("# of processors =", dll.cpuh_get_number_of_processors())
print("processor id =", dll.cpuh_get_process_affinity())
print("process affinity =", dll.cpuh_get_processor())
print("thread affinity =", dll.cpuh_get_thread_affinity())

Linux screenshot

CPU THREAD AFFINITY HELPER in action

License

MIT

Free Software, Hell Yeah!

About

C++ header-only utility for query or change thread affinity in a process

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published