Thread: Simple timer function in c (Needs improvement)

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    2

    Lightbulb Simple timer function in c (Needs improvement)

    I am 17 years old and I'm just getting into c and c++.
    I recently wrote a timer function that, when given an
    input will wait. I am here to ask all you programming wizards and witches to help me find a better way. Here's the code:

    Code:
    #include <time.h>
    
    void Tmr(int len){
        int _s1; //Second 1
        int _s2; //Second 2 
        int _lc; //loop count
           while(_lc != len){ 
                _s1 = time(NULL);  
              if(_s1 != _s2){
                    _lc = _lc + 1;
                    _s2 =  time(NULL); 
                      }
                }
            } 
    int main()
    {
    Tmr(10); //ten seconds or so
    }
    By the way, I am running Windows 8, my IDE is wxDev-C++, compiler set is Default GCC, and my compiler is MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider the non-standard but Posix standard function named sleep. It might not be available via MinGW though, in which case you could try the Windows API version named Sleep. Note though that sleep takes number of seconds as the argument whereas Sleep takes number of milliseconds as the argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I would use the time_t struct defined in <time.h> (or <ctime.h>). That way you might be able to use difftime() to control what breaks the loop: difftime - C++ Reference .

    I would do it by declaring two time_t structures. Then initialize one of them outside the while loop in your function. The other time_t can be constantly updated within the loop, while you compare the return from difftime() to the number passed in.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: I would call it a "delay" function instead of a "timer" function.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Kriptik_nooby View Post
    I am 17 years old and I'm just getting into c and c++.
    Pick one - they're radically different languages. What you write in C is not C++, and what you write in C++ is not C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2014
    Posts
    2
    I want to avoid WinAPI as much as I can.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kriptik_nooby
    I want to avoid WinAPI as much as I can.
    There is no facility in the C++ standard library for getting the process to wait. What you can do is to define your own delay function that has the same interface as the Posix sleep function and implement it using Sleep. If for some reason neither sleep nor Sleep is available, you can substitute the implementation with whatever wait function is available, or implement a busy wait as in your initial idea. This way, you reduce your dependency on the Windows API and can more easily make your program cross platform.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by laserlight View Post
    There is no facility in the C++ standard library for getting the process to wait.
    You could always use an std::condition_variable and invoke the wait_for or wait_until methods with a suitable timeout / time point.


    EDIT: Or better yet, use std::this_thread::sleep_for or std::this_thread::sleep_until.
    Last edited by antred; 09-09-2014 at 09:41 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
    Also, don't name all your variables with a leading underscore.
    The GNU C Library: Reserved Names
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Simple Alarm Timer
    By SirPrattlepod in forum C Programming
    Replies: 8
    Last Post: 11-02-2013, 07:13 AM
  2. Stopwatch or Timer in a simple C program
    By DaniiChris in forum C Programming
    Replies: 6
    Last Post: 07-10-2008, 02:28 AM
  3. Problem with simple timer function
    By mike_g in forum C Programming
    Replies: 16
    Last Post: 01-15-2008, 12:16 PM
  4. Can a DLL use timer function?
    By yunicholas in forum C Programming
    Replies: 5
    Last Post: 12-08-2005, 07:45 PM
  5. Simple Timer, Call external program,
    By bliss in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2005, 11:21 PM

Tags for this Thread