Sleep();

This is a discussion on Sleep(); within the Windows Programming forums, part of the Platform Specific Boards category; I just have a few questions about the 'Sleep' function. 1. Is the number an exact time? Or is how ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,656
    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,893
    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,893
    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
    Posts
    23,020
    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,893
    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
    Posts
    23,020
    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,893
    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,266
    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
    Posts
    23,020
    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.

Page 1 of 2 12 LastLast
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, 03: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, 07: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, 09: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

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