单线程和多线程代码需要同时花费时间

Single-threaded and multi-threaded code taking the same time

本文关键字:费时间 多线程 代码 单线程      更新时间:2023-10-16

我一直在使用 pthreads,但已经意识到如果我使用 1 个线程或将任务分成 1/N 个线程,我的代码独立花费的时间相同。为了举例说明,我将代码简化为此示例:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <boost/progress.hpp>
#define SIZEEXEC 200000000
using namespace boost;
using std::cout;
using std::endl;
typedef struct t_d{
   int  intArg;
} Thread_data;
void* function(void *threadarg)
{
    Thread_data *my_data= (Thread_data *) threadarg; 
    int size= my_data->intArg;
    int i=0;
    unsigned rand_state = 0;
    for(i=0; i<size; i++) rand_r(&rand_state);
    return 0;
}
void withOutThreads(void)
{
    Thread_data* t1= new Thread_data();
    t1->intArg= SIZEEXEC/3;
    function((void *) t1);
    Thread_data* t2= new Thread_data();
    t2->intArg= SIZEEXEC/3;
    function((void *) t2);
    Thread_data* t3= new Thread_data();
    t3->intArg= SIZEEXEC/3;
    function((void *) t3);    
}
void withThreads(void)
{
    pthread_t* h1 = new pthread_t;
    pthread_t* h2 = new pthread_t;
    pthread_t* h3 = new pthread_t;
    pthread_attr_t* atr = new pthread_attr_t;
    pthread_attr_init(atr);    
    pthread_attr_setscope(atr,PTHREAD_SCOPE_SYSTEM);
    Thread_data* t1= new Thread_data();
    t1->intArg= SIZEEXEC/3;
    pthread_create(h1,atr,function,(void *) t1);
    Thread_data* t2= new Thread_data();
    t2->intArg= SIZEEXEC/3;
    pthread_create(h2,atr,function,(void *) t2);
    Thread_data* t3= new Thread_data();
    t3->intArg= SIZEEXEC/3;
    pthread_create(h3,atr,function,(void *) t3);
    pthread_join(*h1,0);
    pthread_join(*h2,0);
    pthread_join(*h3,0);
    pthread_attr_destroy(atr);
    delete h1;
    delete h2;
    delete h3;
    delete atr;
}
int main(int argc, char *argv[])
{
    bool multThread= bool(atoi(argv[1]));
    if(!multThread){
        cout << "NO THREADS" << endl;
        progress_timer timer;
        withOutThreads();
    }
    else {
        cout << "WITH THREADS" << endl;
        progress_timer timer;
        withThreads();
    }
    return 0;
}
要么

代码错误,要么我的系统上有一些东西不允许并行处理。我运行的是 Ubuntu 11.10 x86_64-linux-gnu、gcc 4.6、英特尔®至强(R) CPU E5620 @ 2.40GHz × 4

感谢您的任何建议!

编辑:鉴于答案,我意识到 (1) progress_timer计时器不允许我测量"实时"时间的差异,以及 (2) 我在"函数"中给出的任务似乎不足以让我的机器用 1 或 3 个线程给出不同的时间(这很奇怪,在这两种情况下我都有大约 10 秒......我试图分配内存并使其更重,是的,我看到了差异。虽然我的其他代码更复杂,但它很有可能仍然以 1 或 3 个线程同时运行 +-。谢谢!

这是

意料之中的。您正在测量 CPU 时间,而不是挂机时间。

time ./test 1
WITH THREADS
2.55 s

real    0m1.387s
user    0m2.556s
sys     0m0.008s

实时时间小于用户时间,这与您的测量时间相同。实时是您的挂钟显示的内容,用户和系统是在用户和内核模式下花费的 CPU 时间由所有 CPU 组合而成

time ./test 0
NO THREADS
2.56 s

real    0m2.578s
user    0m2.560s
sys     0m0.008s

您测量的时间,实时时间和用户时间几乎都相同。

罪魁祸首似乎是progress_timer的,或者更确切地说是理解它。

尝试用这个替换 main()。这告诉程序不需要progress_timer报告的时间,也许它报告了总系统时间?

#include <sys/time.h>
void PrintTime() {
    struct timeval tv;
    if(!gettimeofday(&tv,NULL))
        cout << "Sec=" << tv.tv_sec << " usec=" << tv.tv_usec << endl ;
}
int main(int argc, char *argv[])
{
    bool multThread= bool(atoi(argv[1]));
    PrintTime();
    if(!multThread){
        cout << "NO THREADS" << endl;
        progress_timer timer;
        withOutThreads();
    }
    else {
        cout << "WITH THREADS" << endl;
        progress_timer timer;
        withThreads();
    }
    PrintTime();
    return 0;
}