【问题标题】:Using available physical CPU core in pthread in Linux在 Linux 的 pthread 中使用可用的物理 CPU 内核
【发布时间】:2011-10-27 05:30:06
【问题描述】:

我正在使用 Linux Centos。

我正在使用 pthread 编写 c 代码并使用 pthread_set_affinity。我希望看到每个线程在唯一的处理器中运行。就像---

thread1 in proccessor 0
thread2 in proccessor 1
.
.
.
threadn in processor n

目前,当我使用此 set_affinity 时,它会运行,但当我看到系统监视器时,我发现八核的 CPU 利用率没有分布。

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. 

注意:线程是分布式分配的,如 20 20 20 20 ...20

百分比是这样的

10% 1% 0% 2% 0% 1% 0% 0% 1%

我有 8 核 pc 并试图在 8 核中运行 8 线程。有没有办法在这个 pthread 和 pthread_set_affinity 中完全利用 8 核。

【问题讨论】:

  • 可能是一些线程是cpu密集型的,一些线程正在休眠或等待互斥锁,等待阻塞的IO调用。
  • 也许你可以展示一些代码,然后有人可能会提供比概括假设更多的东西。
  • 如果线程在它们之间使用公共数据,这实际上可能不是一个好主意,因为那时数据必须在缓存之间复制,而不是全部在一个缓存中。
  • Joachim Pileborg @ 他们没有使用公共数据。每个线程使用不同的数据,但使用互斥锁的方法相同。

标签: c pthreads cpu-usage


【解决方案1】:

您可能没有足够的工作来完全使用线程。您不需要设置相关性来执行此操作,只需生成 8 个实际上可以 100% 使用单个线程的线程。

【讨论】:

    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    相关资源
    最近更新 更多