绑定,在线程函数内使用
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(6, &mask); //6指核心编号
pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask);
查询线程绑定,在线程函数内使用
cpu_set_t get;
CPU_ZERO(&get);
if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0)
{
fprintf(stderr, "get thread affinity failed\n");
}
for (int i = 0; i < 20; i++)
if (CPU_ISSET(i, &get))
{
printf("thread %ld is running in processor %d\n", pthread_self(), i);
}