我有一个应用程序,其中pthread_join
是瓶颈。我需要帮助来解决这个问题。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;
}
在线程功能打印的时间为40毫秒范围 - 60ms的其中在主功能打印的时间是在650ms - 670ms。具有讽刺意味的是,我的串行代码在650ms - 670ms时间内运行。我能做些什么来减少pthread_join
所花费的时间?
在此先感谢!
16 * 40ms = 640ms。我怀疑这是巧合。你有多少个核心? – ildjarn 2012-01-31 22:30:20
打印出calc_corr中的所有开始和结束时钟,并查看第一次开始时钟和最后一次结束时钟之间的区别。我敢打赌,你会发现大部分时间都花在等待至少一个或多个线程上。 – Arelius 2012-01-31 22:31:51
我有8个内核,我使用pthread_setaffinity_np为每个内核绑定2个线程。 – akhil28288 2012-01-31 22:32:08