简体   繁体   English

在Linux中的pthread中使用可用的物理CPU内核

[英]Using available physical CPU core in pthread in Linux

I am using Linux Centos. 我正在使用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--- 我正在使用pthread在c语言中进行编码,并使用pthread_set_affinity 。我希望看到每个线程都在唯一的处理器中运行。

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. 当前,当我使用此set_affinity时,它会运行,但是当我看到系统监视器时,我看到八核的CPU利用率未分配。

CODE is like that :: CODE就像这样::

        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 注意:线程的分配方式类似于20 20 20 20 ... 20

the percentage is like that way 百分比就是这样

10% 1% 0% 2% 0% 1% 0% 0% 1% 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. 我有八核PC并试图在8核中运行8线程。有没有办法在此pthread和pthread_set_affinity中完全利用8核。

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. 您不必为此设置亲和力,只需生成8个实际上可以100%利用单个线程的线程即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM