在 Linux 中的 pthread 中使用可用的物理 CPU 核心
我使用的是Linux Centos。
我正在使用 pthread 进行 c 语言编码,并使用 pthread_set_affinity。我希望看到每个线程在唯一的处理器中运行。就像——
thread1 in proccessor 0
thread2 in proccessor 1
.
.
.
threadn in processor n
目前,当我使用这个 set_affinity 时,它会运行,但是当我看到系统监视器时我看到八个核心的CPU利用率不是分布的。
代码是这样的 ::
if(for thread 1 )
{
pthread_attr_init(&pta);
CPU_SET(0,&cpuset);
pthread_setaffinity_np(thread1, sizeof(cpu_set_t), &cpuset);
pthread_create(&thread1,&pta,&sendimsg,(void*)&message);
pthread_join(thread1,NULL);
printf("User for Thread One : %d\n",numb.fir);
pthread_attr_destroy(&pta);
CPU_ZERO(&cpuset);
}
else if(for thread 2)
{
pthread_attr_init(&pra);
CPU_SET(1,&cpuset1);
pthread_setaffinity_np(thread2, sizeof(cpu_set_t), &cpuset);
pthread_create(&thread2,&pra,&sendimsg,(void*)&message);
pthread_join(thread2,NULL);
printf("User for Thread Two : %d\n",numb.sec);
pthread_attr_destroy(&pra);
CPU_ZERO(&cpuset1);
}
for eight thread that way.
注意:线程是分布式分配的,如 20 20 20 20 ...20
百分比是这样的
10% 1% 0% 2% 0% 1% 0% 0% 1%
我有八核电脑并尝试运行8个核心中的8个线程。有没有办法在这个pthread和pthread_set_affinity中完全利用8个核心。
I am using Linux Centos.
I am coding in c with pthread and using pthread_set_affinity.I want to see each thread to run in unique processor.Like---
thread1 in proccessor 0
thread2 in proccessor 1
.
.
.
threadn in processor n
currently when i use this set_affinity it runs but when i saw the System-monitor i see that CPU utilization of eight core is not distributed.
CODE is like that
::
if(for thread 1 )
{
pthread_attr_init(&pta);
CPU_SET(0,&cpuset);
pthread_setaffinity_np(thread1, sizeof(cpu_set_t), &cpuset);
pthread_create(&thread1,&pta,&sendimsg,(void*)&message);
pthread_join(thread1,NULL);
printf("User for Thread One : %d\n",numb.fir);
pthread_attr_destroy(&pta);
CPU_ZERO(&cpuset);
}
else if(for thread 2)
{
pthread_attr_init(&pra);
CPU_SET(1,&cpuset1);
pthread_setaffinity_np(thread2, sizeof(cpu_set_t), &cpuset);
pthread_create(&thread2,&pra,&sendimsg,(void*)&message);
pthread_join(thread2,NULL);
printf("User for Thread Two : %d\n",numb.sec);
pthread_attr_destroy(&pra);
CPU_ZERO(&cpuset1);
}
for eight thread that way.
Note : thread are assigned distributedly like 20 20 20 20 ...20
the percentage is like that way
10% 1% 0% 2% 0% 1% 0% 0% 1%
I have eight core pc and trying to run 8 thread in 8 core.Is there a way to completly utilize 8 core in this pthread and pthread_set_affinity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能没有足够的工作来完全使用线程。您不需要设置亲和力来执行此操作,只需生成 8 个线程,这些线程实际上可以 100% 使用单个线程。
You probably don't have enough work to use the threads completely. You shouldn't need to set the affinity to do this, just spawn off 8 threads that can actually 100% utilize a single thread.