Thread: Help!!! I need a pure C timer!

  1. #1
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27

    Exclamation Help!!! I need a pure C timer!

    I wanna a timer that works in pure C program (e.g. static library). It need not be accuracy, so simple is the best. My platform is Windows + MinGW. I've tried stantard Win32 timer, but it does not satisfy me because my program does not have a window. And I also have searched on web for a long time, but only lots of C++ timer classes found. So I have to ask this here. Any guides or useful infomation will be appreciated. Thanks in advance.
    Last edited by TalosChen; 05-07-2006 at 08:32 AM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    #include <time.h> /* Standard: for clock stuff */
    
    void wait(double seconds)
    {
       time_t start = clock();
       while((clock() - start) < seconds * CLOCKS_PER_SEC)
        {
            /* do nothing */
        }
    }

  3. #3
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    Yeah, I think code above is simple. But I also wanna know if there is timers using Callback functions (like standard Win32 timer)?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not that I know of unless you just really want to use Sleep() or something like that, but that is not pure C. What I gave you is.

  5. #5
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    Sorry, there's one thing that I forgot to say, that is, I dont wanna a WAIT timer. What I really need is a TIMEOUT timer. For example, the socket function recv(). When it works in block mode, I want it to time out without setting the socket timeout property (for some reason).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://cboard.cprogramming.com/showthread.php?t=41926
    You might want to find the "beej" networking tutorial, in particular,
    - how to make a non-blocking stream
    - how to use select() to implement timeouts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    But if in general, how can I implement a timeout timer or something else?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Use the wait timer to call exit() if it finishes or something. You can break the loop if the operation you are really trying to perform succeeds. For instance maybe:
    Code:
    void timeout(double seconds, FN_PNTR)
    {
       time_t start = clock();
       while ((clock() - start) < seconds * CLOCKS_PER_SEC)
       {
           if ( ! FN_PNTR) /* if the operation fails based on the return value*/
              continue;
           else
              break; /* it worked */
        }
        exit(0); /* timed out... leave the program */
    }
    Where FN_PNTR is a real function pointer
    Therefore the system has so long to try something.
    Last edited by whiteflags; 05-07-2006 at 11:09 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And if that function never returns for you to test how much time has elapsed, what do you do then?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    Quote Originally Posted by Salem
    And if that function never returns for you to test how much time has elapsed, what do you do then?
    Agree. That's the essence of the problem, what if the function never returns? I think another thread will be needed, but that complicates the problem and a lot should be learned by me. So is there any method to avoid thread programming, even it's platform related?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is this is a general question, or are you trying to solve an actual problem?

    Perhaps state the actual problem and we can offer alternatives which may not involve timers at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    The actual problem is already solved by using select(). But I still wanna know how to implement such a timer that I described. I'm really interesting about it.

  13. #13
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    Can somebody be kind enough to recommand some good timer tutorials (about how to implement, not to use) to me?

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Google and man are the essence of research.

  15. #15
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I've always done it with threads. The basic idea is you maintain a list of actions (callbacks), sorted by time (shortest time first). Then you have a thread that waits on the shortest time in the queue, when it wakes up it runs the action (in another thread, so that the action being run doesn't affect timers that still need to be run), removes the front entry, and waits on the next entry in the queue.

    When most entries are added to the queue, it's not a problem, you just need to make sure that you lock the queue before you add. The special case is a head insertion, because that means the wait thread needs to wait on a smaller timeout value.

    My solution was always to use pthread_cond_timedwait on unix, or WaitForSingleObject on windows, to have a timeout that can be signalled from another thread. When the wait thread wakes up, you can detect if if was from a timeout (run the action) or from a signal (rewait on the head of queue).
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling IRichEditOle interface methods
    By Niara in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 01:23 PM
  2. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  3. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  4. Information Regarding Pure Virtual Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2003, 04:43 AM