PhoenixThread  1.0.0
Tools to ease parallel programming in C++
Loading...
Searching...
No Matches
pin_thread_to_core.h File Reference
#include <stdio.h>
#include <stdlib.h>
+ Include dependency graph for pin_thread_to_core.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PIN_THREAD_TO_CORE   if(!pinThreadToCore())exit(1);
 

Functions

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

Macro Definition Documentation

◆ PIN_THREAD_TO_CORE

#define PIN_THREAD_TO_CORE   if(!pinThreadToCore())exit(1);

Definition at line 16 of file pin_thread_to_core.h.

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.