Thread: Alternative to sleep()

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    Alternative to sleep()

    Hi, I need a function that will keep the program inactive for n milliseconds. sleep() in unistd.h does this in seconds. I searched google but didn't find much (1 that didn't work and had C++ code too).
    I am programming for Windows, so I don't need to worry about portability.

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    never mind, I found one function Sleep() that takes milliseconds. But I'm not sure which header it's included in.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Header

    Declared in Winbase.h; include Windows.h.
    http://msdn2.microsoft.com/en-us/library/ms686298.aspx

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Abda92 View Post
    Hi, I need a function that will keep the program inactive for n milliseconds. sleep() in unistd.h does this in seconds. I searched google but didn't find much (1 that didn't work and had C++ code too).
    I am programming for Windows, so I don't need to worry about portability.

    Thanks
    The most portable method of sub-second waiting is select(). Not a standard C function, but available on all UNIX as well as Windows.

    Simply pass an empty set of descriptors and set the timeout to the value you want.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    The most portable method of sub-second waiting is select(). Not a standard C function, but available on all UNIX as well as Windows.

    Simply pass an empty set of descriptors and set the timeout to the value you want.
    Wow, I thought select() was just used for networking. Although isn't usleep() fairly common on UNIX?

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, 06: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, 08:21 AM
  4. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM