PhoenixThread  1.0.0
Tools to ease parallel programming in C++
Loading...
Searching...
No Matches
pin_thread_to_core.cpp
Go to the documentation of this file.
1
2/***************************************
3 Auteur : Pierre Aubert
4 Mail : pierre.aubert@lapp.in2p3.fr
5 Licence : CeCILL-C
6****************************************/
7
8#include <errno.h>
9#include <pthread.h>
10#include <iostream>
11#include "pin_thread_to_core.h"
12
13#define handle_error_en(en, msg) \
14 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
15
17
20#ifndef __APPLE__
21 cpu_set_t cpuset;
22 pthread_t thread = pthread_self();
23 /* Set affinity mask to include CPUs 0 to 7 */
24
25 CPU_ZERO(&cpuset);
26 for(int j = 0; j < 8; j++){
27 CPU_SET(j, &cpuset);
28 }
29 int s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
30 if(s != 0){handle_error_en(s, "pthread_setaffinity_np");return false;}
31 /* Check the actual affinity mask assigned to the thread */
32 s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
33 if(s != 0){handle_error_en(s, "pthread_getaffinity_np");return false;}
34#endif
35 return true;
36}
37
38
bool pinThreadToCore()
Pins the current thread to the current core.
#define handle_error_en(en, msg)