1

I have implemented a lock free linked list data structure. I am trying to measure the time taken by the program. I have 4 threads. The threads are implemented using pthreads library. Currently I am measuring the time using the following method

#include <time.h>
/*
Some code
.........
*/
pthread_t t1,t2;
clock_t begin = clock();
pthread_create (&t1, NULL, thread1, (void *)mylist);
pthread_create (&t2, NULL, thread2, (void *)mylist);
pthread_create (&t3, NULL, thread3, (void *)mylist);
pthread_create (&t4, NULL, thread4, (void *)mylist);
pthread_join (t1, NULL);
pthread_join (t2, NULL);
pthread_join (t3, NULL);
pthread_join (t4, NULL);
clock_t end= clock();
double time_s = (double)(end-begin)/ CLOCKS_PER_SEC;

I thought that this would give me correct result. However when I measured time taken to individual threads like this

void * thread1(void * args)
{
    clock_t begin = clock();
    /* Some Code */
    clock_t end= clock();
    double time_s = (double)(end-begin)/ CLOCKS_PER_SEC;
}

The timing was different. These are the reading that I got:

Total time: 
2.06341

Thread 1
Time: 0.767887

Thread 2
Time:1.20169

Thread 3
Time:1.92999

Thread 4
Time:2.0584

Why is this happening? Is this correct? Any help would be appreciated.

Sumant
  • 77
  • 6
  • (depending on platform) You could try using `clock_gettime()` with `CLOCK_THREAD_CPUTIME_ID` if you're after CPU time used by each thread, or `CLOCK_PROCESS_CPUTIME_ID` for the total for whole process... – Dmitri Jul 13 '16 at 21:58
  • why do you think that it is not correct? – tony Jul 16 '16 at 01:57

0 Answers0