Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: sleep(int) in C++?

  1. #11
    Join Date
    Jun 2006
    Beans
    943

    Re: sleep(int) in C++?

    Well, if you're using SDL, you could try SDL_Delay:

    http://www.libsdl.org/cgi/docwiki.cgi/SDL_Delay

    If you use nanosleep, be careful with resources (I've removed my code as it encourages poor use of this function):

    Quote Originally Posted by LibC
    This function is a cancellation point in multi-threaded programs. This is a problem if the thread allocates some resources (like memory, file descriptors, semaphores or whatever) at the time nanosleep is called. If the thread gets canceled these resources stay allocated until the program ends. To avoid this calls to nanosleep should be protected using cancellation handlers.
    http://www.gnu.org/software/libc/man....html#Sleeping
    Last edited by Lster; August 13th, 2008 at 03:03 PM.

  2. #12
    Join Date
    Jul 2008
    Beans
    1,491

    Re: sleep(int) in C++?

    Would there be anything wrong with using the Linux API? I thought there were C functions built into the OS for this kind of trick? wait([args]) ?

    Yep, sure looks like it:

    http://linux.about.com/od/commands/l/blcmdl2_wait.htm
    http://www.computerhope.com/unix/uwait.htm

  3. #13
    Join Date
    Sep 2007
    Location
    Location: Location:
    Beans
    95
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: sleep(int) in C++?

    Thanks. I just wanted to make sure that it was a standard library function since I don't have a Windows machine to test it on since my PC's monitor died.

    EDIT: The sleep() function in "time.h" takes float values! But I'm not sure whether or not that's just Xcode...
    Last edited by Kopachris; August 14th, 2008 at 04:56 AM.
    Registered Linux user #481459

  4. #14
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: sleep(int) in C++?

    Quote Originally Posted by Lster View Post
    Well, if you're using SDL, you could try SDL_Delay:

    http://www.libsdl.org/cgi/docwiki.cgi/SDL_Delay

    If you use nanosleep, be careful with resources (I've removed my code as it encourages poor use of this function):



    http://www.gnu.org/software/libc/man....html#Sleeping
    I'm sleepy and dehydrated... what causes that?? Hmmm??

    Anyhow, Lster, are you answering your original question yourself?

  5. #15
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: sleep(int) in C++?

    Quote Originally Posted by Kopachris View Post
    Thanks. I just wanted to make sure that it was a standard library function since I don't have a Windows machine to test it on since my PC's monitor died.

    EDIT: The sleep() function in "time.h" takes float values! But I'm not sure whether or not that's just Xcode...
    Thanks to who? And for what?

    If you want, you can pass a double into the sleep() function or whatever (see a bogus example below). But seriously, it only takes unsigned integers, representing the number of seconds, as described in the API. Any float or double will be rounded off (that is, down) to form an unsigned integer. Thus 5.99999 will be 5 seconds.
    PHP Code:
    #include <unistd.h>

    int main()
    {
      
    double val 5.99999;
      
    sleepval );
      return 
    0;

    Last edited by dwhitney67; August 14th, 2008 at 06:19 AM.

  6. #16
    Join Date
    Sep 2007
    Location
    Location: Location:
    Beans
    95
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: sleep(int) in C++?

    It was originally thanks to anyone and everyone, I guess. Who thought of accepting double values and rounding it down? Why not just accept ints? Nanosleep doesn't work, I looked at it and (in the Mac standard library, at least) it doesn't have much to do with "sleep" at all. Hod139's sleep thing works pretty well, although it's dependent on how long the clock ticks are on the machine it runs on and takes longer depending on how fast your computer can decide whether or not "goal" is still grater than "clock()". On my machine, this rounds out to about 1/100th of a second for one cycle (might have something to do with having iTunes running). Ah, well, it should be good enough.

    Boring, you-don't-care-about stuff:
    But that sleep function is very similar to how you would make a TI 83-84+ graphing calculator sleep. In TI BASIC, the calculator executes about 1 instruction per millisecond, so doing "FOR(x,0,1000):END" is a good way to get it to sleep for about a second.
    Registered Linux user #481459

  7. #17
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: sleep(int) in C++?

    Below is an example program employing nanosleep(). If the program receives a signal, then nanosleep() will return before the time has expired (this is according to the API). Thus in the program below I have demonstrated that nanosleep() can be called again after an expected signal has been received/processed.

    PHP Code:
    #define _POSIX_C_SOURCE 199309
    #include <ctime>
    #include <cerrno>
    #include <csignal>
    #include <cstring>
    #include <cstdlib>

    #include <iostream>

    using namespace std;


    void sigHandlerint signum )
    {
      
    cout << "caught signal " << signum << endl;
    }


    void sleepFortime_t secondslong nsecs )
    {
      
    timespec sleepPeriod = { secondsnsecs };
      
    timespec unusedPeriod;

      for (;;)
      {
        if ( 
    nanosleep( &sleepPeriod, &unusedPeriod ) == )
        {
          break;
        }
        else if ( 
    errno == EINTR )
        {
          
    cout << "sig detected... resuming timer." << endl;
          
    sleepPeriod unusedPeriod;
        }
        else
        {
          
    cerr << "Error occurred with nanosleep; reason = " << strerrorerrno ) << endl;
          break;
        }
      }
    }


    int mainint argcchar** argv )
    {
      
    time_t seconds 5;
      
    long   nsecs   0;

      if ( 
    argc )
      {
        
    seconds atoiargv[1] );
      }
      if ( 
    argc )
      {
        
    nsecs atolargv[2] );
      }

      
    signalSIGUSR2sigHandler );

      
    sleepForsecondsnsecs );

      
    cout << "time up!" << endl;

      return 
    0;

    P.S. After compiling the program above, run it. While it is running, send a USR2 signal to it to see the program output that the nanosleep (which was interrupted) is being resumed.
    Code:
    $ g++ nanosleep.cpp -o nanosleep
    $ nanosleep 60 &
    $ kill -s USR2 `pidof nanosleep`
    Last edited by dwhitney67; August 14th, 2008 at 04:01 PM. Reason: Added instructions on how to run the program.

  8. #18
    Join Date
    Feb 2006
    Location
    Seattle-Eastside
    Beans
    309
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: sleep(int) in C++?

    Quote Originally Posted by hod139 View Post
    There is not a c++ standard sleep (there is a posix standard shown above), but it is easy enough to write it yourself:

    Code:
    #include <time.h>
     
    void sleep(unsigned int mseconds)
    {
        clock_t goal = mseconds + clock();
        while (goal > clock());
    }
    Not my code, credit goes to: http://www.dreamincode.net/code/snippet38.htm
    This is a busy loop and thankfully NOT how the real sleep() works. The real sleep uses signals and signal handlers.

  9. #19
    Join Date
    Feb 2006
    Location
    Seattle-Eastside
    Beans
    309
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: sleep(int) in C++?

    Oops! dwhitney67 already beat me to it.

  10. #20
    Join Date
    Sep 2007
    Location
    Location: Location:
    Beans
    95
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: sleep(int) in C++?

    hod139's loop sleeper will be good enough. I just need it to execute all it's instructions in a nice, even amount of time to make calculations consistent. I don't necessarily need it to not use the CPU much since it's only going to be sleeping for probably a few hundredths of a second while it's not doing anything anyway. Thanks for your help, everyone!
    Registered Linux user #481459

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •