Created
January 4, 2015 02:13
-
-
Save benwills/5f77566f628c9e83ab40 to your computer and use it in GitHub Desktop.
Assign a thread to a specific cpu core. From http://stackoverflow.com/questions/1407786/how-to-set-cpu-affinity-of-a-particular-pthread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// core_id = 0, 1, ... n-1, where n is the system's number of cores | |
int stick_this_thread_to_core(int core_id) { | |
int num_cores = sysconf(_SC_NPROCESSORS_ONLN); | |
if (core_id < 0 || core_id >= num_cores) | |
return EINVAL; | |
cpu_set_t cpuset; | |
CPU_ZERO(&cpuset); | |
CPU_SET(core_id, &cpuset); | |
pthread_t current_thread = pthread_self(); | |
return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment