An alternative wait function?

This is a discussion on An alternative wait function? within the C++ Programming forums, part of the General Programming Boards category; Hey, I just joined the forums and I thought I'd dive straight in with a query. I am searching for ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    An alternative wait function?

    Hey, I just joined the forums and I thought I'd dive straight in with a query. I am searching for a function that will wait, say 5 seconds, and then proceed with whatever comes next. For example:

    cout << "Please wait\n";

    <Some sort of pause function>

    cout << "Did you enjoy your wait?\n";


    Apparently, wait just waits until background processes are finished. The exact definition I found was "With no arguments, the wait command waits until all background processes known to the current shell have terminated".

    Are there some arguments I need to add, or is a totally different function needed?

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Eg.
    cout << "hello\n";
    sleep(5);
    cout << "world\n";
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    The <time.h> lib. can do that. wait (int); where int is equal to seconds.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Quote Originally Posted by Salem
    Eg.
    cout << "hello\n";
    sleep(5);
    cout << "world\n";
    When I use this I get the error " `sleep' cannot be used as a function ". I also get the same for wait.

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Include windows.h for Sleep().

    I wonder why some professionals try to avoid Sleep function?

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    I wasn't aware of Sleep() (and about a million other snipets of code).

    Wait (); doesn't work by itself. I somewhat assumed that you'd go research.

    Code:
    void wait ( int seconds )
    {
      clock_t endwait;
      endwait = clock () + seconds * CLK_TCK ;
      while (clock() < endwait) {}
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Of course, actually posting
    a) your code
    b) your OS/Compiler
    might make it a lot easier to figure out what your question is.

    And when I say code, I mean all of it, not random single lines.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The bad thing with the wait function is that it is a tight loop that uses up all free CPU resources. Sleep may only work for Windows, but at least it doesn't use CPU to do nothing.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There is no portable solution that doesn't use busy waiting (i.e. a loop that just executes until a certain point in time is reached, which is bad because it hogs the CPU for no reason).

    Therefore, in this case it would be better to write a small wait() function yourself that calls different functions depending on the platform.
    xp_sleep.h:
    Code:
    void xp_sleep(unsigned int seconds);
    void xp_millisleep(unsigned int milliseconds);
    xp_sleep_win32.cpp:
    Code:
    #include <windows.h>
    void xp_sleep(unsigned int seconds)
    {
      Sleep(seconds * 1000);
    }
    
    void xp_millisleep(unsigned int milliseconds)
    {
      Sleep(milliseconds);
    }
    xp_sleep_posix.cpp:
    Code:
    #define _POSIX_C_SOURCE 199309
    #include <errno.h>
    #include <time.h>
    
    void xp_sleep(unsigned int seconds)
    {
      struct timespec ts = {
        seconds,
        0L
      };
      while(nanosleep(&ts, &ts) == -1 && errno == EINTR) {
      }
    }
    void xp_millisleep(unsigned int milliseconds)
    {
      struct timespec ts = {
        milliseconds / 1000,
        (milliseconds % 1000) * 1000000L
      };
      while(nanosleep(&ts, &ts) == -1 && errno == EINTR) {
      }
    }
    Then use the posix implementation for all Unix variants (including Mac OS X) and the win32 for Windows.

    Usage:
    Code:
    #include "xp_sleep.h"
    
    int main()
    {
      xp_sleep(5);
      return 0;
    }
    Note: although this is the C++ forum, I've intentionally kept this code fully compatible with both C and C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    5
    Unfortunately I was under the impression it would be as simple as wait(5). Hehe, well, I'll leave it until I get a bit more familiar with C++. Thanks everyone.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you don't care about portability and want to work it with Windows, you can just call the Sleep() function. It would be good to know that your program cannot be compiled for other OS without modifications, but I guess for a new-comer and a learner supporting all platforms is not so important.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    There is an FAQ on the subject . . . http://faq.cprogramming.com/cgi-bin/...&id=1043284392 . . . it has less information than this thread, though.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 11:08 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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