Something better than 'Sleep'?

This is a discussion on Something better than 'Sleep'? within the C++ Programming forums, part of the General Programming Boards category; I'm working on a 'quiz game' which contains a fair amount of text (introduction, questions, hints etc.) and I'd really ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    Something better than 'Sleep'?

    I'm working on a 'quiz game' which contains a fair amount of text (introduction, questions, hints etc.) and I'd really appreciate some tips on how to stagger the appearance of the text without requiring the user to 'press any key to continue'.

    Code:
    cout <<"So would you like to begin the game?\n\n";
                            Sleep (600);
                            cout << "The rules of the game are very simple:\n";
                            Sleep (600);
    As a make-do, I'm using the 'Sleep' function from the windows.h library, but I know this makes the program non-portable, as well as hearing some generally bad comments about the actual processes involved.

    Is there a more portable, less troubling version of this function? I'd like my text to appear a line at a time, or if it's possible, a letter at a time (similar to the way text often appears in modern console RPGs).

    (I'm working on the same project that I was a week ago in this thread, if that matters.)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I can't imagine what would be troubling about the "actual processes" involved. The whole point of Sleep is that there are no other "actual processes" involved -- it just tells the computer to go do whatever else needs to be done for .6 seconds. Sleep is the only legitimate way to do an automatic pause. (Now, technically, you may wait a little longer than .6 seconds, but I can't see how that's an issue for you either.) If you need multi-platform whatever, you can do conditional compilation with #if this #else that in the preprocessor.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    It was a few weeks ago, so I could be misremembering or misinterpreting what was said, but I think there were complaints about the fact that the amount of time you dictate is the shortest period it will sleep for. Apparently in some freak cases, it can cause the computer to pause for a length of time that's pretty much an equivalent to total standstill. Then again, I could be talking out of my arse, so apologies if I am. =/

  4. #4
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    What I used to do was basically kill time with for loops. So I would make a pause function go for some amount of time like this:
    Code:
    void pause(int x);
    {
         for(int i = 0; i < x; i++)
         ;
    }
    I would just make x some huge number, obviously inside the boundary's of an int. It worked for making it show up letter by letter. And it is portable. Though I don't think the processor gets freed. It is actively incrementing the counter in the for loop.

    Hope that helps.
    -- Will you show me how to c++?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I can't imagine it doing that unless the whole computer has come to a standstill with the workload (but I may be wrong).

    I rather think that Sleep might not be that advisable in real-time games where you don't want to miss frames or give up valuable 10 ms for logic processing (but then I haven't written such a big game where I would have felt that 100% CPU usage is justified, only to keep the timing as precise as possible.)

    I would just make x some huge number
    You do realize that this loop would run for a very different amount of time on different computers, if the compiler doesn't optimize it away completely?

    If you want to generate heat with busy loops, at least check against elapsed time.
    Last edited by anon; 09-18-2009 at 05:08 PM.
    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).

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Angie View Post
    It was a few weeks ago, so I could be misremembering or misinterpreting what was said, but I think there were complaints about the fact that the amount of time you dictate is the shortest period it will sleep for. Apparently in some freak cases, it can cause the computer to pause for a length of time that's pretty much an equivalent to total standstill. Then again, I could be talking out of my arse, so apologies if I am. =/
    Well, yes. (I phrased it as "a little longer", but if the processor catches fire it may take some time before your program is resumed.) I'll put it this way: although I am aware of the potential, I scoff at the suggestion to use a counting loop to to do a busy-wait. (After all, I'm still going to get scheduled in and out, am I not? So if this misbehaving rogue program that is also running is going to cause a hang, it's going to cause a hang whether I hog the resources or not; it just may get a chance to do it sooner.)

  7. #7
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    So in short you are saying that no matter what, since we are going to get scheduled, that what ever function you use isn't going to be exact? (as far as timing goes)
    -- Will you show me how to c++?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Regarding the original question: what I like to do is to provide several versions of the code. Use Sleep() in Windows; usleep() in Linux; delay() in DOS (if you care about DOS compatibility ); a busy-loop if none of those apply. You can write something like this to do the trick:
    Code:
    #ifdef WIN32
    #include <windows.h>
    void pause_ms(unsigned milliseconds) {
        Sleep(milliseconds);
    }
    #elif defined(unix)
    #include <unistd.h>
    void pause_ms(unsigned milliseconds) {
        usleep(milliseconds * 1000);
    }
    #else
    void pause_ms(unsigned milliseconds) {
        clock_t start = clock();
        while(start + (milliseconds / 1000.0 * CLOCKS_PER_SEC) < clock()) {}
    }
    #endif
    You get the idea. Not sure what macro would be good to use to see whether usleep exists, but "unix" is a start, anyway. Not sure if my logic in the busy-loop is right or not either, you'd have to try it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I just use Boost. I believe it does basically the same thing dwks posted (might use win32 api instead, I dunno).

    Code:
    boost::this_thread::sleep(boost::posix_time::milliseconds(100));
    Do not use a loop.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    You could also...
    Code:
    DWORD StartTime = GetTickCount();
    int someInt = 6;
    
    //do business
    
    while ((GetTickCount() - Start Time) < someInt);

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Angie View Post
    It was a few weeks ago, so I could be misremembering or misinterpreting what was said, but I think there were complaints about the fact that the amount of time you dictate is the shortest period it will sleep for. Apparently in some freak cases, it can cause the computer to pause for a length of time that's pretty much an equivalent to total standstill. Then again, I could be talking out of my arse, so apologies if I am. =/
    It will never be a total standstill, but yes, sleep may wait a few ms longer than specified, as when the timer runs out another thread may have priority, in which case the sleeping thread will have to wait its turn as normal. All other methods will do the same exact thing though, its a feature of preemptive multitasking OS's. If you need exactly precise timing you need to run a real time OS.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Platform-dependent preprocessor defines -
    Pre-defined C/C++ Compiler Macros

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by mramazing View Post
    What I used to do was basically kill time with for loops. So I would make a pause function go for some amount of time like this:
    Code:
    void pause(int x);
    {
         for(int i = 0; i < x; i++)
         ;
    }
    I would just make x some huge number, obviously inside the boundary's of an int. It worked for making it show up letter by letter. And it is portable. Though I don't think the processor gets freed. It is actively incrementing the counter in the for loop.

    Hope that helps.
    No it can't possibly help because that suggestion is absurd! That abomination hasn't worked for decades! You're dreaming if you think it is the least bit feasible.
    A modern compiler will optimise out the loop entirely causing zero delay, and any that don't will cause 100% CPU usage for vastly different amounts of time on each and every PC. It isn't "portable" in any sense of the word.
    I'd seriously consider firing anyone who used that for anything but a spinlock.

    When the original poster spoke of Problems with Sleep, they were referring to hypothetical issues that don't occur in practice. In practice Sleep works like a charm. Ditto what abachler says.

    Boost is the platform independent method.
    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"

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by iMalc View Post
    It isn't "portable" in any sense of the word.
    Well, its portable in that it works equally badly on every platform
    Quote Originally Posted by iMalc View Post
    I'd seriously consider firing anyone who used that for anything but a spinlock.
    I wouldn't even think once, that code isn't even good for a spinlock. I'd at least have the decency to tell the guy not to use me as a reference.
    Last edited by abachler; 09-19-2009 at 12:01 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by abachler View Post
    well, its portable in that it works equally badly on every platform :d

    i wouldn't even think once, that code isn't even good for a spinlock. I'd at least have the decency to tell the guy not to use me as a reference.
    LOL
    Last edited by Sebastiani; 09-19-2009 at 12:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 09:17 PM
  2. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 07:46 AM
  3. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 09:21 AM
  4. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM
  5. sleep
    By ivandn in forum Linux Programming
    Replies: 7
    Last Post: 12-13-2001, 11:20 PM

Tags for this Thread


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