Obtaining thread Core affinity in C++ 11 through pthreads
Asked Answered
V

2

8

I'm trying to set core affinity (Thread #1 goes on first core, Thread #2 goes on second core, ...) while using std::thread in C++ 11.

I've already searched around various topics and on the internet and it seems C++ 11 API doesn't provide such low level feature.

On the other hand, pthreads come with pthread_setaffinity_np which would be useful if I could get the "pthread_t" value of my std::thread (I don't know if this is human reasonable or at least legitimate asking for it).

An example program of what I'd want to have in the end is this:

#include <thread>
#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define CORE_NO 8

using namespace std;

void run(int id) {
    cout << "Hi! I'm thread " << id << endl;
    // thread function goes here
}

int main() {
    cpu_set_t cpu_set;

    CPU_ZERO(&cpu_set);
    for(int i=0; i<CORE_NO; i++)
        CPU_SET(i, &cpu_set);

    thread t1(run, 1);

    // obtaining pthread_t from t1

    /*
    pthread_t this_tid = foo(t1);
    pthread_setaffinity_np(this_tid, sizeof(cpu_set_t), &cpu_set);
    */

    t1.join();

    return 0;
}

I'd really prefer not to change the whole architecture of my project (which must provide such characteristic). I've now a massive use of std::thread but I can use pthread API in addition as well, as you have seen in the example.

Is there a way for me to solve this problem?

Vanatta answered 16/4, 2013 at 10:17 Comment(3)
'which must provide such characteristic' - what performance improvement have you seen after setting the thread affinity? Do you have any numbers?Fullmouthed
@Martin James - seconded. Rob013 - In my experience you have to have a very particular set of circumstances before messing around with core affinity beats what the OS will automatically do anyway. And you then have to have a load of code to work out at runtime the optimum affinity for threads on whatever hardware the program is being run on, and that's a load of hassle. You're probably better off letting the OS do its best to begin with. If you really need core affinity then you'll probably also need memory affinity to get any benefit, and that'll screw your architecture completely anyway.Plebe
Thanks for your advice. I see your point of view, I'm pretty sure OS thread management would be better than a student who learned what core affinity is just a couple days ago. By the way, my question was about a specific problem inside a porting project so I was only told "we use core affinity. You implement it". And for my goals Joachim's solution is quite enough.Vanatta
E
10

You can get the native handle for the thread with the native_handle function.

The example in the linked reference even uses this to call pthread functions.

Everlasting answered 16/4, 2013 at 10:23 Comment(1)
Yes, but as far as I can tell that that's not guaranteed to be of type pthread_t. PThreads themselves are quite often a layer on top of the underlying OS's native threads. Linux with NPTL is probably the exception. So you'll need OS specific routines to handle the core affinity.Plebe
C
7

I do not know if it is a suitable approach in your case, but what I usually do is to call the affinity primitives from within the thread. E.g., I place a snippet of code similar to this one somewhere at the beginning of the threaded function:

const int err = pthread_setaffinity_np(pthread_self(),...);

The call to pthread_self() will return the ID of the calling thread.

Cyrene answered 16/4, 2013 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.