Skip to content

Instantly share code, notes, and snippets.

@benwills
Created January 4, 2015 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benwills/5f77566f628c9e83ab40 to your computer and use it in GitHub Desktop.
Save benwills/5f77566f628c9e83ab40 to your computer and use it in GitHub Desktop.
// 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