Thread: timers in c

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    timers in c

    Hello,

    I am just wondering if there are any timers in c standard library.

    I just need a simple timer that will start and stop.
    example. Enter the function and start the timer. After a certain condition
    stop the timer.

    Is there anytime I can use it C?

    Many thanks,

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    there is data type of time_t in c standard library.i think what u're looking for is this
    Code:
    time_t start,end;
    start=clock();//predefined  function in c
    //after the user defined function does its work
    end=clock();
    t=(end-start)/CLOCKS_PER_SEC;
    actually i'm also not familiar with this function in great depth so if anyone further rectifies me if i'm wrong.
    thank you

  3. #3
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    I think what I am really looking for is a simple stopwatch application.

    Start and then after 10 seconds stop.

    This is what I am currently working on.

    Thanks,

  4. #4
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    There is a good example I am following here.

    I will show you my solution when finished.

    http://www.cplusplus.com/reference/c...ime/clock.html

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That solution is actually quite BAD. It consumes full use of the CPU for the entire time of the waiting period.

    If you want to WAIT for x amount of time, there are system-dependant functions that do that for you. Sleep(milliseconds) in Windows, usleep(microseconds) in most forms of Unix. These calls will allow the CPU to work on other things whilst your application is waiting.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    I have decided to use the sample code. However, I will run the start timer in another thread using pthread.

    However, when I run the application it doesn't run for 10 seconds as it should. And it doesn't seem to go into the g_start_timer.

    The value I always get returned is: 0. Which cannot be correct. Can anyone see any improvement in my code, as I have done something wrong.

    many thanks,

    Code:
    #include <stdio.h>
    #include "stopwatch_timer.h"
    int main()
    {
        printf("Start your timers\n");
        
        /* start the time and run for 10 seconds */
        start_timer(10);
    
        int i = 0; 
        /* do some work */
        for(i = 0; i < 1000; i++){}
    
        /* Check and display current time */
        printf("Curent time: %d\n", current_time());
    
        return 0;
    }
    Code:
    /* stopwatch_timer.c */
    #include <pthread.h>
    #include <time.h>
    #include <stdio.h>
    
    #include "stopwatch_timer.h"
    
    /* prototypes */
    int* g_start_timer(void *secs);
    
    static clock_t _current_time = 0;
    
    /* create the thread */
    void start_timer(int seconds)
    {
        pthread_t thread_id;
        int rc = 0;
    
        rc = pthread_create(&thread_id, NULL, g_start_timer, (void*) seconds);
    
        if(rc)
        {
    	printf("=== Error Creating thread\n");
        } 
    }
    
    /* start the timing in another thread */
    int* g_start_timer(void *secs)
    {
        printf("Starting thread\n");
        int seconds = (int) secs;
        printf("g_start_timer: %d\n", (int) seconds);
      
      _current_time = clock() + seconds * CLOCKS_PER_SEC;
    
        /* loop until the 10 seconds has reached */
        while(clock() < _current_time){}
    
        pthread_exit(NULL);
    }
    
    /* get the current time of work */
    int current_time()
    {
        return (int) _current_time / CLOCKS_PER_SEC;
    }
    Code:
    /* stopwatch_timer.h */
    
    void start_timer(int seconds);
    int current_time();

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    The value I always get returned is: 0.
    Your main thread does nothing - so it exits immediatly, why do you expect something else?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    UK2
    Join Date
    Sep 2003
    Posts
    112
    hello,

    Thanks for the reply.

    I have changed my main to add the pthread_exit(NULL)

    It now runs for 10 seconds.

    However, I would have thought I wouldn't have managed to get the current time. Is this possible with what I am trying to do.

    I thought with running in another thread. The for loop would act like it is doing some work. So when I check the time. I can get the current time which the g_start_timer function is up to.

    Code:
    int main()
    {
        printf("Start your timers\n");
        
        /* start the time and run for 10 seconds */
        start_timer(10);
    
        int i = 0; 
        /* do some work */
        for(i = 0; i < 100000; i++){}
    
        /* Check and display current time */
        printf("Curent time: %d\n", current_time());
    
        pthread_exit(NULL);
    
        return 0;
    }

  9. #9
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Thanks for the response.

    I am wondering is it possible and I have never done this before, that is to create a callback. So after a interval as expired it will call this call back function.

    If you could point me in the right direction.

    Many thanks,

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes it's possible. If you are creating a thread, then you could pass in a function pointer as (part of) the parameter(s) to the thread creation. Then do something like:
    Code:
        delay(x seconds);
        call through function pointer.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    I have just created a simple program, as this is my first time with threads and callbacks.
    However, I am wondering do I really need a callback. Can I just call the timeout_cb() function
    after the sleep() as completed? Maybe I don't understand, why a callback is needed at all?

    Just one more question. The time could be set for a long as 30 seconds. However, the task might
    complete before then. Is there any way to stop the timer. As if the task has been completed before
    the 30 seconds, I don't want the call back to be called at all? What is I need is stop_timer.

    I was thinking is there anyway to stop the sleep once it has started?

    Many thanks for any suggestions,

    Code:
    #include <pthread.h>
    #include <stdio.h>
    
    /* call back function - inform the user the time has expired */
    void timeout_cb()
    {
        printf("=== your time is up ===\n");
    }
    
    /* Go to sleep for a period of seconds */
    static void* g_start_timer(void *args)
    {
        /* function pointer */
        void (*function_pointer)();
    
        /* cast the seconds passed in to int and 
         * set this for the period to wait */
        int seconds = *((int*) args);
        printf("go to sleep for %d\n", seconds);
        
        /* assign the address of the cb to the function pointer */
        function_pointer = timeout_cb;
        
        sleep(seconds);
        /* call the cb to inform the user time is out */
        (*function_pointer)();
    	
        pthread_exit(NULL);
    }
    
    int main()
    {
        pthread_t thread_id;
        int seconds = 3;
        int rc;
    
        rc =  pthread_create(&thread_id, NULL, g_start_timer, (void *) &seconds);
        if(rc)
    	printf("=== Failed to create thread\n");
    
        pthread_join(thread_id, NULL);
    
        printf("=== End of Program - all threads in ===\n");
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  2. Enumerating timers and hotkeys
    By maxorator in forum Windows Programming
    Replies: 3
    Last Post: 01-01-2007, 11:47 AM
  3. switch cases and timers
    By soranz in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 06:43 PM
  4. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 04:45 PM
  5. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM