C++ 11.
私はすでにさまざまなトピックやインターネットを検索しましたが、C++ 11 API はそのような低レベルの機能を提供していないようです。
一方、pthreads にはpthread_setaffinity_npが付属しています。これは、std::thread の "pthread_t" 値を取得できる場合に役立ちます (これが人間の合理的な要求なのか、少なくとも正当な要求なのかはわかりません)。
私が最終的にやりたいことのサンプルプログラムはこれです:
#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;
}
プロジェクトのアーキテクチャ全体を変更しないことを本当に望んでいます(そのような特性を提供する必要があります)。私は現在 std::thread を大量に使用していますが、例で見たように、さらに pthread API を使用することもできます。
この問題を解決する方法はありますか?