pthread_join是一个瓶颈

akh*_*288 2 c++ performance multithreading pthreads

我有一个应用程序,pthread_join它是瓶颈.我需要帮助来解决这个问题.

void *calc_corr(void *t) {
         begin = clock();
         // do work
         end = clock();
         duration = (double) (1000*((double)end - (double)begin)/CLOCKS_PER_SEC);
         cout << "Time is "<<duration<<"\t"<<h<<endl;
         pthread_exit(NULL);
}

int main() {
         start_t = clock();

         for (ii=0; ii<16; ii++) 
            pthread_create(&threads.p[ii], NULL, &calc_corr, (void *)ii);

         for (i=0; i<16; i++) 
            pthread_join(threads.p[15-i], NULL);

         stop_t = clock();

         duration2 = (double) (1000*((double)stop_t - (double)start_t)/CLOCKS_PER_SEC);
         cout << "\n Time is "<<duration2<<"\t"<<endl;

         return 0;
}
Run Code Online (Sandbox Code Playgroud)

螺纹功能中打印的时间范围为40ms - 60ms,主要功能中打印的时间为650ms - 670ms.具有讽刺意味的是,我的串行代码运行时间为650毫秒 - 670毫秒.我该怎么做才能减少所需的时间pthread_join

提前致谢!

Mys*_*ial 10

在Linux上,clock()测量组合的CPU时间.它不测量墙壁时间.

这就解释了为什么你会得到~640 ms = 16 * 40ms.(正如评论中所指出)

要测量墙壁时间,您应该使用以下内容: