简体   繁体   中英

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!

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:

By creating some threads you are adding an overhead to your system: Creation time, scheduling time. Creating a thread require allocating the stack, etc; scheduling means more context switching. Also, pthread_join suspends execution of the calling thread until the target thread terminates . Which means you want for thread 1 to finish, when he does you are rescheduled as quick as possible but not instantly, then you wait for thread 2, etc...

Now your computer has few cores, like one or 2, and you are creating 16 threads. At best 2 threads of your program will run at the same time and just by adding their clock measurements you have something around 400 ms .

Again It depends on lot of things, so I quickly flown over what is happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM