PhoenixThread  1.0.0
Tools to ease parallel programming in C++
Loading...
Searching...
No Matches
pin_thread_to_core.cpp File Reference
#include <errno.h>
#include <pthread.h>
#include <iostream>
#include "pin_thread_to_core.h"
+ Include dependency graph for pin_thread_to_core.cpp:

Go to the source code of this file.

Macros

#define handle_error_en(en, msg)
 

Functions

bool pinThreadToCore ()
 Pins the current thread to the current core.
 

Macro Definition Documentation

◆ handle_error_en

#define handle_error_en ( en,
msg )
Value:
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

Definition at line 13 of file pin_thread_to_core.cpp.

13#define handle_error_en(en, msg) \
14 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

Referenced by pinThreadToCore().

Function Documentation

◆ pinThreadToCore()

bool pinThreadToCore ( )

Pins the current thread to the current core.

Returns
true on success, false otherwise

Definition at line 19 of file pin_thread_to_core.cpp.

19 {
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}
#define handle_error_en(en, msg)

References handle_error_en.