Like Tree2Likes

Minute Delay Loop

This is a discussion on Minute Delay Loop within the C++ Programming forums, part of the General Programming Boards category; I'm needing minutes to always be 00 seconds. But after running my code for a few minutes, it jumps to ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    93

    Minute Delay Loop

    I'm needing minutes to always be 00 seconds. But after running my code for a few minutes, it jumps to 58 seconds. I cannot see any reason why. Anyone have any ideas? Thanks.

    Code:
    #include <windows.h>
    #include <ctime>
    #include <iostream>// cout
    
    using namespace std;
    
    int main(){
    	string dataStr;
    	clock_t cSecs; time_t tSecs;
    	int Secs,elapsedTime=0,iStall=1;
    	Secs=(60-((int)time(0)%60))*1000;// first 00
    	bool looping=true;
    	while(looping){
    		Sleep(Secs); cSecs=clock(); tSecs=time(0);
    		char timestamp[20]; struct tm pSecs;
    		localtime_s(&pSecs,&tSecs);
    		strftime(timestamp, 20, "%X", &pSecs);
    		cout << Secs <<" + "<< elapsedTime <<"\t= "<< timestamp <<"\n";
    		Sleep(753*(iStall%5)); iStall++;
    		elapsedTime=(clock()-cSecs)*1000/CLOCKS_PER_SEC;
    		Secs=(60000-elapsedTime);
    	}
    	return 0;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Sleep is not millisecond accurate, nor should you rely on it to be.
    It will sleep for >= specificed milliseconds, depending on circuimstances.
    Salem likes this.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Unfortunately this eats up the CPU:
    Code:
    	do{
    		elapsedTime=(clock()-cSecs)*1000/CLOCKS_PER_SEC;
    	} while(elapsedTime<60000);
    Any suggections?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Don't rely on having to do something exactly n ms. Use timers or Sleep to approximately do something every n ms.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Hey, that's fine with me. I've actually already invisioned doing this... very ugly stuff so far. I've tried modulus math tricks and a Sleep(500) loop with time(0) check release.

    But, I really do not know of a clever K.I.S.S. way that works to get 00 seconds every time (in sync with the computer clock). I would still love any suggestions.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Why do you need to do that?
    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.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    It's part of a time sync tool used to monitor the NYSE.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Anybody else?

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Does it have to be exactly every n milliseconds or is it okay for it to be n +/- m milliseconds where m is small?
    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.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    I really only need it to be 00 seconds.
    It doesn't matter too much if it is 00+few extra millis (even up to 00.999), but not 59 or 01 seconds.
    Thanks for your help.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    By the way, I'm currently testing this:
    Code:
    Sleep((60-((int)time(0)%60))*1000);
    iSecs=((int)time(0)%60); if(iSecs>40) Sleep((60-iSecs)*1000);
    And, it seems to be good, but I find that hard to believe since that would be way too KISS

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    time only has second accuracy. You might try clock() or GetTickCount() which accuracy of some milliseconds.
    The basic principle is then simply to try to compensate for "oversleep."
    For example, take clock() before, Sleep for n milliseconds.
    Take clock() again and calculate how many milliseconds it "overslept."
    Then subtract that from the amount to sleep next time, and so on.
    It will you an average of n millisecond sleep each time, but not perfect.
    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
    Join Date
    Feb 2010
    Posts
    93
    Earlier, you said: "It will sleep for >= specificed milliseconds"
    And now, you use the word "oversleep".
    Do you mean that I should expect random increased Sleep millis each cycle?

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Something like this?
    Code:
    while (looping)
    {
         int time_of_next_00_in_ms = ((current_time_in_ms() / (1000*60)) + 1) * (1000*60);
         sleep_ms(time_of_next_00_in_ms - current_time_in_ms());
         ....
    }
    Basically round current time to last time second was 00, add 1 minute, gives the time you should sleep until.

    Then, you can just sleep the time between the calculated time and current time.

    This should give you 00 most of the time. Unfortunately, since Windows is not a real-time system (meaning it doesn't make any timing guarantees), theoretically, if it's very busy, it can decide to not schedule (run) your process for as long as it wants.

    If timing is VERY critical (eg. in a life support device, or vehicle computer), a real time system should be used, but that is a lot of hassle, and may not be practical. There is a kernel patch that makes Linux real time, but I'm not aware of any real time variant of Windows. Another option is to just accept that your code will work 99.99% of the time, and make sure nothing catastrophic will happen if it oversleeps.

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Do you mean that I should expect random increased Sleep millis each cycle?
    The only guarantee Sleep(ms) gives you is that, your thread/process will be suspended for AT LEAST ms. Theoretically, Windows can suspend your process for 2 years even if you only asked for 2 seconds. Not likely, but Windows doesn't guarantee it won't do that.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wait just a minute here!!!
    By Kennedy in forum C Programming
    Replies: 7
    Last Post: 04-13-2009, 09:36 AM
  2. While Loop and Delay
    By hmd in forum C Programming
    Replies: 5
    Last Post: 02-25-2008, 12:34 PM
  3. Minute to hour/minute conversion program
    By Remius in forum C Programming
    Replies: 7
    Last Post: 12-29-2007, 07:39 AM
  4. 12 minute timer
    By Gone in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2003, 03:30 PM
  5. Delay A While Loop
    By SonicWave in forum C++ Programming
    Replies: 4
    Last Post: 09-09-2001, 10:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21