Thread: Sleep();

  1. #1
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114

    Sleep();

    I just have a few questions about the 'Sleep' function.

    1. Is the number an exact time? Or is how long it lasts judged on CPU speed?
    2. Is it possible to have a variable in the brackets instead of a number? Like
    Code:
    Sleep(x);
    ?
    3. (not really related to Sleep) is it possible to create a popup message that opens up over all the other windows? Like a custom error message popup?

    Answers appreciated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by bradszy View Post
    I just have a few questions about the 'Sleep' function.

    1. Is the number an exact time? Or is how long it lasts judged on CPU speed?
    2. Is it possible to have a variable in the brackets instead of a number? Like
    Code:
    Sleep(x);
    ?
    3. (not really related to Sleep) is it possible to create a popup message that opens up over all the other windows? Like a custom error message popup?

    Answers appreciated.
    1. It is time - but not exact - it will not depend on CPU speed, but will depend on CPu usage by other thread/processes, so Sleep can take more when asked

    Also Sleep(1) will probably take about 15 ms because it is the "real" resolution of Sleep

    2. Yes, as any other function

    3. System Modal dialog?

    Such posts are really should be done on Windows programming - because Sleep and Messagebox are Windows specific
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I agree this should be in a windows programming forum, not C++.

    The resolution of Sleep() is actually CPU dependent. 15ms is a rule of thumb for some older processors, assuming a single processor machine.

    The literal working of Sleep() is that it causes the calling thread to yield it's timeslice, and the windows scheduler does not allocate another timeslice during the specified period. So, the argument of Sleep() specifies an approximate lower bound on the time a thread is effectively suspended. The scheduler may elect not to allocate a timeslice (eg if there is significant demand from higher priority threads) so, theoretically, there is no upper bound.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) In other words, the OS puts the thread to sleep for a minimum of the time specified. Once the time is up, normal scheduling resumes, which means that your thread is woken up whenever the OS feels like it - if a high-priority process is hogging the CPU, that may not be for a long time.

    And yes, Windows board.
    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

  5. #5
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Thanks guys, but 1 more thing, is it possible to use both MB_ICONWARNING and MB_SYSTEMMODAL together? Like having the warning icon as well as having system modal properties? Thanks again.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Have you tried?

    You link the flags with the bitwise or operator |.
    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

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sleep can also sleep less than the specified interval in some cases. But it sleeps roughly the amount of time specified by the code. And of course you can pass a variable with the time to the function.
    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.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It can?
    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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I actually complained to Microsoft about the inconsistencies of the Sleep documentation, but under remarks on MSDN, you'll find:
    If dwMilliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time.
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I see. Interesting.
    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

  11. #11
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Sleep() is good enough for most applications, however for multimedia purposes (say you're making a media player) you're probably going to need a higher-resolution timer. The multimedia timer functions will go some way to accomplishing that (Unless Raymond Chen has said anything different, it is independent of the task scheduler).

    On modern processors you can also use the performance counter.

  12. #12
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    I tested it on my PC while switching ie pages, opening folders, opening Photo Shop CS3,and it gave me EXACTLY 1 minute... I know the results will vary from PC to PC, but it's consistent enough.
    Also, corned, you were right, I had to put | in between them. Thanks everyone, my problems are solved.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh but a minute is a lot of milliseconds. It works on milliseconds, so it can be pretty consistent there.
    It's 60 000 milliseconds, so it's not exactly that difficult to reach that target. Plus it probably overslept some milliseconds too.
    Did you check 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.

  14. #14
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    How would I check that?

  15. #15
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    <ctime> will be of use to you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [pthread] cancellable sleep in win32?
    By cyberfish in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2007, 02:30 AM
  2. 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
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 06:46 AM
  4. 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
  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