Thread: i swear i'm "special" somtimes.

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    i swear i'm "special" somtimes.

    what's the best way to get something to happen every say, 60 seconds? i've been trying with
    Code:
      if ((clock () /  CLOCKS_PER_SEC) % 60 == 0){
    from time.h but i'm not sure if that's the best way to do it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You're right, it isn't.

    clock() measures CPU time, not wall-clock time, so unless you're thrashing the CPU, that's gonna lag.
    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.

  3. #3
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Well i was trying to use
    Code:
    time_t  start, end;
    , and then something like
    Code:
     
    while(true) {
                    end = Not sure what to put here.
                    if ((end - start) ==0){
                             //do somthing
                    }
    }

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Look up difftime

    And you probably want to compare "now" (time(0)) with start to see if the difference is (greater or ?) equal to 60.0.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or if you want other applications to run whilst you are "waiting", use something like Sleep() [windows, 60000 will make 1 minute] or sleep() [Linux/Unix etc, 60 will make one minute].

    The OS will then take care of figuring out the time it needs to wake up again. It will be fairly precise, no less so than what you get if you spin in user-mode. The main difference is that if you spin in user-mode, the OS may think "CPU-intensive task", and put your task at a lower priority, whilst a "waiting" task will get raised priority. So it's likely to be MORE precise.

    --
    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
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    ahh got it;

    Code:
        
    const int TimeElapse = 10; // amount of time to wait, in seconds.
    time_t now = time(0); // set the now time.
        while(true)
            if ((time(0)-now) % TimeElapse == 0) //comepare now to the current time, and modulus for zero.
                cout << time(0) << endl; // test std::cout
    obviously that's not exactly the code i'll use, but it shows that the if works.
    Last edited by Terran; 05-26-2008 at 04:23 PM.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You better hope nobody adjusts their computer clock whilst that code's running!
    Also, if there is a momentary delay such as when a page of virtual memory is loaded from the page file, then you could miss one of your periodic tasks.
    You must not look for a specific time. Instead, you'd wait until the time has been reached (or exceeded) and then remember that you have done the task for that minute and wait for the next one.

    The best way to do a periodic task really depends on the operating system (please state it).
    In a Windows app, using a timer to generate WM_TIMER messages is a good approach, for example.
    One could instead use GetTickCount() with code similiar to what you're posted, to avoid problems if your clock is changed.
    Last edited by iMalc; 05-27-2008 at 01:07 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I have a non CPU-eating function (that you could modify) that can be used to sleep or lock to a certain FPS, so to speak.
    http://cboard.cprogramming.com/showp...91&postcount=1
    If you're interested.
    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.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Title of your thread is a little too unobviously clear to me

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    53
    If you really care about correctness, use difftime(). time_t is not guaranteed to be in seconds (although it almost always is).

    It is not considered very nice to perform "busy waiting", but like matsp pointed out, the way to perform a "sleep" is unfortunately (but understandibly) not standard across platforms.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  11. #11
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    it's not like just eating time though. Say i want the user to still be able to use the menu functions while the program is still processing actions in the back and a certain action (say population of a planet) doubles every minute.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This calls for a threaded approach. One threads runs a message loop and blocks until messages are received and one thread performs all the other work you want.
    This will keep the interface smooth while you can still sleep/wait in the other thread.
    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.

  13. #13
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    guess i have to learn how to sew then!

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    We still don't know what OS yet.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Oh, well Vista for the current sys i program on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Last Time with this Program ... I swear!
    By DonFord81 in forum C Programming
    Replies: 18
    Last Post: 03-16-2009, 01:10 PM
  2. Omg Last Time I Swear!
    By unejam2005 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2005, 12:49 AM
  3. One last thing i swear!!
    By fingerlickin in forum C# Programming
    Replies: 3
    Last Post: 08-19-2005, 01:25 PM
  4. One Last Question....I swear (its due at midnight!)
    By LittleLotte in forum C Programming
    Replies: 6
    Last Post: 02-02-2005, 09:04 PM
  5. Swear filter in a chat room.
    By adrianxw in forum Tech Board
    Replies: 9
    Last Post: 10-30-2003, 06:40 AM