PhoenixMicroBenchmark  1.0.1
Toolset for function micro-benchmarking
Loading...
Searching...
No Matches
pin_thread_to_core.cpp File Reference
#include <errno.h>
#include <stdio.h>
#include <stdlib.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 15 of file pin_thread_to_core.cpp.

15#define handle_error_en(en, msg) \
16 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 23 of file pin_thread_to_core.cpp.

23 {
24#ifndef __APPLE__
25 int s, j;
26 cpu_set_t cpuset;
27 pthread_t thread;
28
29 thread = pthread_self();
30
31 /* Set affinity mask to include CPUs 0 to 7 */
32
33 CPU_ZERO(&cpuset);
34 for (j = 0; j < 8; j++)
35 CPU_SET(j, &cpuset);
36
37 s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
38 if (s != 0){
39 handle_error_en(s, "pthread_setaffinity_np");
40 return false;
41 }
42 /* Check the actual affinity mask assigned to the thread */
43
44 s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
45 if (s != 0){
46 handle_error_en(s, "pthread_getaffinity_np");
47 return false;
48 }
49#endif
50 return true;
51}
#define handle_error_en(en, msg)

References handle_error_en.