2

I want to implement a timer in C, after a particular time a function is to be called. I want to implement the timer separately using a thread. I observed that the timer runs at twice the speed when it is implemented in a thread as opposed to when it is implemented in the main function.

Why does this happen? How can I fix it?

Code 1(without thread):

int main (){
    unsigned int x_hours=0;
    unsigned int x_minutes=0;
    unsigned int x_seconds=0;
    unsigned int x_milliseconds=0;
    unsigned int totaltime=0,count_down_time_in_secs=0,time_left=0, prev_time =10;    
    clock_t x_startTime,x_countTime;
    count_down_time_in_secs=20; 
    x_startTime=clock();  // start clock
    time_left=count_down_time_in_secs-x_seconds;   

    while (time_left>0) 
    {
        x_countTime=clock(); // update timer difference

        x_milliseconds=x_countTime-x_startTime;
        x_seconds=(x_milliseconds/(CLOCKS_PER_SEC))-(x_minutes*60);
        x_minutes=(x_milliseconds/(CLOCKS_PER_SEC))/60;
        x_hours=x_minutes/60;
        time_left=count_down_time_in_secs-x_seconds; // subtract to get difference
        if(time_left-prev_time != 0)
        { 
            //printf("\nx_countTime = %ju\n",x_countTime);
            prev_time = time_left;
            //printf( "\nYou have %d seconds left ( %d ) count down timer by TopCoder",time_left,count_down_time_in_secs);
        }   
    }
    printf( "\n\n\nTime's out\n\n\n");
    return 0;
}

Code 2(with timer)

void *timerThread(void *param){
    unsigned int x_hours=0;
    unsigned int x_minutes=0;
    unsigned int x_seconds=0;
    unsigned int x_milliseconds=0;
    unsigned int totaltime=0,count_down_time_in_secs=0,time_left=0,prev_time =10;
    clock_t x_startTime,x_countTime;
    count_down_time_in_secs=20;  
    x_startTime=clock();  // start clock
    time_left=count_down_time_in_secs-x_seconds;

    while (time_left>0) 
    {
        x_countTime=clock(); // update timer difference

        x_milliseconds=x_countTime-x_startTime;
        x_seconds=(x_milliseconds/(CLOCKS_PER_SEC))-(x_minutes*60);
        x_minutes=(x_milliseconds/(CLOCKS_PER_SEC))/60;
        x_hours=x_minutes/60;
        time_left=count_down_time_in_secs-x_seconds; // subtract to get difference
        if(time_left-prev_time != 0)
        { 
            printf("\nx_countTime = %ju\n",x_countTime);
            prev_time = time_left;
            printf( "\nYou have %d seconds left ( %d ) count down timer by TopCoder",time_left,count_down_time_in_secs);
        }   
    }
    printf( "\n\n\nTime's out\n\n\n");
    exit(1);
}

int main()
{
    pthread_t *thread_id;
    pthread_create(&thread_id, NULL, timerThread, NULL);
    while(1);
}
K. Kirsz
  • 1,384
  • 10
  • 11

1 Answers1

2

Be careful because the time measured by two calls to clock measures different things on different platforms. On Unix and Unix-like systems (like for example macOS and Linux) the difference is the process CPU time, while on Windows it's based on the wall-clock.

As for your problem I guess you're on a Unix or Unix-like system. The reason I think that is just because the difference between two clock calls is the elapsed CPU time for a process, and if you use threads then it might seem that the time is speeding up because of that (since the threads run in parallel). This is the same reason you can have a multi-threaded process use more than 100% CPU time.

The solution is to use some other way to measure time, something unrelated to CPU times and threads. For example clock_gettime.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621