Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread_join is being a bottleneck

I have an application where pthread_join is being the bottleneck. I need help to resolve this problem.

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;
}

The time printed in the thread function is in the range of 40ms - 60ms where as the time printed in the main function is in the 650ms - 670ms. The irony is, my serial code runs in 650ms - 670ms time. what can I do to reduce the time taken by pthread_join?

Thanks in advance!

like image 214
akhil28288 Avatar asked May 08 '26 23:05

akhil28288


1 Answers

On Linux, clock() measures the combined CPU time. It does not measure the wall time.

This is explains why you get ~640 ms = 16 * 40ms. (as pointed out in the comments)

To measure wall time, you should be using something like:

  • gettimeofday()
  • clock_gettime()
like image 151
Mysticial Avatar answered May 10 '26 14:05

Mysticial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!