linux线程运行指定核,怎么确定 pthread 双线程是在两个核下运行,而不是在一个核(超核)下运行...

C/C++ codeIn the following program, the main thread uses pthread_setaffinity_np() to set its CPU affinity mask to include CPUs 0 to 7 (which may not all be avail-

able on the system), and then calls pthread_getaffinity_np() to check the resulting CPU affinity mask of the thread.

#define _GNU_SOURCE

#include

#include

#include

#include

#define handle_error_en(en, msg) \

do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

int

main(int argc, char *argv[])

{

int s, j;

cpu_set_t cpuset;

pthread_t thread;

thread = pthread_self();

/* Set affinity mask to include CPUs 0 to 7 */

CPU_ZERO(&cpuset);

for (j = 0; j < 8; j++)

CPU_SET(j, &cpuset);

s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);

if (s != 0)

handle_error_en(s, "pthread_setaffinity_np");

/* Check the actual affinity mask assigned to the thread */

s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);

if (s != 0)

handle_error_en(s, "pthread_getaffinity_np");

printf("Set returned by pthread_getaffinity_np() contained:\n");

for (j = 0; j < CPU_SETSIZE; j++)

if (CPU_ISSET(j, &cpuset))

printf(" CPU %d\n", j);

exit(EXIT_SUCCESS);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值