Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sleep() issues

Sleep() issues

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
16 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pblais
    wrote on last edited by
    #1

    Hi... If there is a better way to do what I am doing, I'll jump on it. I am using Sleep(milliseconds) in my code to place strategic waits for time to pass. As an example, I need to stop the application because I know that something going on in the real world will take at least 100 msecs to finish and I don't have anyway to handshake with this device/application. So I put a Sleep(100). This has worked ok until recently when I tried running my app in one of the newer PCs with the real fast processor etc. Now my Sleep()'s are too short. Is there a way to figure out what the Sleep() will be in msecs from one PC to another? Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be? Like I said earlier, if there is a better and more accurate way to do this the using Sleep(), I am open to all suggestions... Regards and thank you in advance Pierre

    C R M S L 6 Replies Last reply
    0
    • P pblais

      Hi... If there is a better way to do what I am doing, I'll jump on it. I am using Sleep(milliseconds) in my code to place strategic waits for time to pass. As an example, I need to stop the application because I know that something going on in the real world will take at least 100 msecs to finish and I don't have anyway to handshake with this device/application. So I put a Sleep(100). This has worked ok until recently when I tried running my app in one of the newer PCs with the real fast processor etc. Now my Sleep()'s are too short. Is there a way to figure out what the Sleep() will be in msecs from one PC to another? Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be? Like I said earlier, if there is a better and more accurate way to do this the using Sleep(), I am open to all suggestions... Regards and thank you in advance Pierre

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Use a timer, a timers interval is set in milliseconds.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      1 Reply Last reply
      0
      • P pblais

        Hi... If there is a better way to do what I am doing, I'll jump on it. I am using Sleep(milliseconds) in my code to place strategic waits for time to pass. As an example, I need to stop the application because I know that something going on in the real world will take at least 100 msecs to finish and I don't have anyway to handshake with this device/application. So I put a Sleep(100). This has worked ok until recently when I tried running my app in one of the newer PCs with the real fast processor etc. Now my Sleep()'s are too short. Is there a way to figure out what the Sleep() will be in msecs from one PC to another? Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be? Like I said earlier, if there is a better and more accurate way to do this the using Sleep(), I am open to all suggestions... Regards and thank you in advance Pierre

        R Offline
        R Offline
        Ravi Bhavnani
        wrote on last edited by
        #3

        That's surprising. I would have thought that calling Sleep(100); would suspend the process for at least (but not necessarily exactly) 100mSec. /ravi

        This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

        1 Reply Last reply
        0
        • P pblais

          Hi... If there is a better way to do what I am doing, I'll jump on it. I am using Sleep(milliseconds) in my code to place strategic waits for time to pass. As an example, I need to stop the application because I know that something going on in the real world will take at least 100 msecs to finish and I don't have anyway to handshake with this device/application. So I put a Sleep(100). This has worked ok until recently when I tried running my app in one of the newer PCs with the real fast processor etc. Now my Sleep()'s are too short. Is there a way to figure out what the Sleep() will be in msecs from one PC to another? Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be? Like I said earlier, if there is a better and more accurate way to do this the using Sleep(), I am open to all suggestions... Regards and thank you in advance Pierre

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          So you are saying that new fast processors alter time? milliseconds are milliseconds (I thought).

          pblais wrote:

          Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be?

          I believe the accuracy of Sleep() can vary from +-10ms to +-55ms, depending on the system. A possibly interesting article: Precision is not the same as accuracy[^]

          P 1 Reply Last reply
          0
          • P pblais

            Hi... If there is a better way to do what I am doing, I'll jump on it. I am using Sleep(milliseconds) in my code to place strategic waits for time to pass. As an example, I need to stop the application because I know that something going on in the real world will take at least 100 msecs to finish and I don't have anyway to handshake with this device/application. So I put a Sleep(100). This has worked ok until recently when I tried running my app in one of the newer PCs with the real fast processor etc. Now my Sleep()'s are too short. Is there a way to figure out what the Sleep() will be in msecs from one PC to another? Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be? Like I said earlier, if there is a better and more accurate way to do this the using Sleep(), I am open to all suggestions... Regards and thank you in advance Pierre

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            I suspect the problem is in the ratio of the “wake time” to the “sleep time”. On faster machines the “wake time” is lower but the “sleep time” remains about the same.

            Steve

            1 Reply Last reply
            0
            • P pblais

              Hi... If there is a better way to do what I am doing, I'll jump on it. I am using Sleep(milliseconds) in my code to place strategic waits for time to pass. As an example, I need to stop the application because I know that something going on in the real world will take at least 100 msecs to finish and I don't have anyway to handshake with this device/application. So I put a Sleep(100). This has worked ok until recently when I tried running my app in one of the newer PCs with the real fast processor etc. Now my Sleep()'s are too short. Is there a way to figure out what the Sleep() will be in msecs from one PC to another? Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be? Like I said earlier, if there is a better and more accurate way to do this the using Sleep(), I am open to all suggestions... Regards and thank you in advance Pierre

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              pblais wrote:

              I don't have anyway to handshake with this device/application.

              pblais wrote:

              when I tried running my app in one of the newer PCs

              pblais wrote:

              Now my Sleep()'s are too short.

              Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded? :rolleyes: Otherwise at some point your application will be Sleep()ing with the fishes.

              led mike

              M P 2 Replies Last reply
              0
              • L led mike

                pblais wrote:

                I don't have anyway to handshake with this device/application.

                pblais wrote:

                when I tried running my app in one of the newer PCs

                pblais wrote:

                Now my Sleep()'s are too short.

                Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded? :rolleyes: Otherwise at some point your application will be Sleep()ing with the fishes.

                led mike

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                led mike wrote:

                Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded?

                And use a trackbar for fine-tuning!

                1 Reply Last reply
                0
                • P pblais

                  Hi... If there is a better way to do what I am doing, I'll jump on it. I am using Sleep(milliseconds) in my code to place strategic waits for time to pass. As an example, I need to stop the application because I know that something going on in the real world will take at least 100 msecs to finish and I don't have anyway to handshake with this device/application. So I put a Sleep(100). This has worked ok until recently when I tried running my app in one of the newer PCs with the real fast processor etc. Now my Sleep()'s are too short. Is there a way to figure out what the Sleep() will be in msecs from one PC to another? Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be? Like I said earlier, if there is a better and more accurate way to do this the using Sleep(), I am open to all suggestions... Regards and thank you in advance Pierre

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #8

                  As has been said, the number you pass to Sleep() is just a request. Your thread might not wake up for a minute if some high-priority thread grabs the CPU. Something more accurate than Sleep() seems to be in order, check out multimedia timers and waitable timers.

                  --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                  1 Reply Last reply
                  0
                  • M Mark Salsbery

                    So you are saying that new fast processors alter time? milliseconds are milliseconds (I thought).

                    pblais wrote:

                    Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be?

                    I believe the accuracy of Sleep() can vary from +-10ms to +-55ms, depending on the system. A possibly interesting article: Precision is not the same as accuracy[^]

                    P Offline
                    P Offline
                    pblais
                    wrote on last edited by
                    #9

                    Mark Salsbery wrote:

                    So you are saying that new fast processors alter time? milliseconds are milliseconds (I thought).

                    Everyone likes a wise cracker

                    M 1 Reply Last reply
                    0
                    • L led mike

                      pblais wrote:

                      I don't have anyway to handshake with this device/application.

                      pblais wrote:

                      when I tried running my app in one of the newer PCs

                      pblais wrote:

                      Now my Sleep()'s are too short.

                      Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded? :rolleyes: Otherwise at some point your application will be Sleep()ing with the fishes.

                      led mike

                      P Offline
                      P Offline
                      pblais
                      wrote on last edited by
                      #10

                      led mike wrote:

                      Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded?

                      I like that idea... How would I make configurable?

                      L 1 Reply Last reply
                      0
                      • P pblais

                        led mike wrote:

                        Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded?

                        I like that idea... How would I make configurable?

                        L Offline
                        L Offline
                        led mike
                        wrote on last edited by
                        #11

                        pblais wrote:

                        How would I make configurable?

                        Instead of Sleep(100) something like this: Sleep( Config.Milliseconds); where the number in that variable is provided by a user or administrator.

                        led mike

                        M 1 Reply Last reply
                        0
                        • P pblais

                          Mark Salsbery wrote:

                          So you are saying that new fast processors alter time? milliseconds are milliseconds (I thought).

                          Everyone likes a wise cracker

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #12

                          :) You're the one that stated "Now my Sleep()'s are too short" If you choose to use a timer then you'll need to use a multimedia timer. Chances are a WM_TIMER through the SetTimer API is going to be just as inaccurate. I agree with another poster that the problem is probably related to the time the thread's awake, not the time it's suspended in Sleep(). Do you do I/O with the external device you are trying to sync to? If so, is there any way you can use overlapped I/O or soething to make it a bit more event driven instead of trying to fine- tune timeslices of threads? Mark

                          P 1 Reply Last reply
                          0
                          • L led mike

                            pblais wrote:

                            How would I make configurable?

                            Instead of Sleep(100) something like this: Sleep( Config.Milliseconds); where the number in that variable is provided by a user or administrator.

                            led mike

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #13

                            :((

                            L 1 Reply Last reply
                            0
                            • M Mark Salsbery

                              :((

                              L Offline
                              L Offline
                              led mike
                              wrote on last edited by
                              #14

                              I wonder if he wants special sauce on his Fish Filet?

                              led mike

                              M 1 Reply Last reply
                              0
                              • L led mike

                                I wonder if he wants special sauce on his Fish Filet?

                                led mike

                                M Offline
                                M Offline
                                Mark Salsbery
                                wrote on last edited by
                                #15

                                :laugh: The whole thing brings back memories of my first computer job - interfacing with a home-brewed laser scanner. All the magic numbers in the legacy code, and how the new 386 CPU messed it all up LOL! To this day, it's still referred to (by myself and my business partner, who worked with me then) as the Laser Bread Slicer (that, we figured, was the extent of its accuracy). So, typical tech support goes something like "See the edit box labeled 'Fudge Factor'? Try entering 203 insead of 198 and see if that fixes it." Thanks man...I thought I had successfully repressed those memories :laugh:

                                1 Reply Last reply
                                0
                                • M Mark Salsbery

                                  :) You're the one that stated "Now my Sleep()'s are too short" If you choose to use a timer then you'll need to use a multimedia timer. Chances are a WM_TIMER through the SetTimer API is going to be just as inaccurate. I agree with another poster that the problem is probably related to the time the thread's awake, not the time it's suspended in Sleep(). Do you do I/O with the external device you are trying to sync to? If so, is there any way you can use overlapped I/O or soething to make it a bit more event driven instead of trying to fine- tune timeslices of threads? Mark

                                  P Offline
                                  P Offline
                                  pblais
                                  wrote on last edited by
                                  #16

                                  Mark Salsbery wrote:

                                  Do you do I/O with the external device you are trying to sync to? If so, is there any way you can use overlapped I/O or soething to make it a bit more event driven instead of trying to fine- tune timeslices of threads?

                                  Yes sometimes. I am writing test software. Sometimes I have to wait for another piece of test equimpmen to go through it's boot up self test and I have no way knowing when it is done. I do know that it takes it approximately 300 mSec. So I put a Sleep(350) in my code to wait for it. I have recently found, on some newer/faster PCs that the application isn't waiting long enough...

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups