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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problems with Sleep() in thread

Problems with Sleep() in thread

Scheduled Pinned Locked Moved C / C++ / MFC
question
29 Posts 9 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.
  • A acerunner316

    I am using Sleep(10) in my thread to post a windows message every 10ms. In the function responding to the windows message, I find that it is being executed approximately every 16ms. Where does the 6ms delay come from? My computer is not the best 1.53GHz, but 6ms delay is still way too slow.

    S Offline
    S Offline
    S Douglas
    wrote on last edited by
    #18

    acerunner316 wrote:

    but 6ms delay is still way too slow

    To get an understanding of how time works read Time is the Simplest Thing[^]


    I'd love to help, but unfortunatley I have prior commitments monitoring the length of my grass. :Andrew Bleakley:

    1 Reply Last reply
    0
    • C Chris Losinger

      he was talking about accuracy in the 10-20ms range. GetTickCount can't get near that.

      image processing toolkits | batch image processing | blogging

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #19

      So.. uh, the system timer resolution is too low? Lucky me that I don't have to do anything multimedia related, because I was under the impression that a timer which is given the attribute system would be the most accurate one. Thanks for the heads up - who knows? I might have to deal with issues like this some day...

      -- Verletzen zerfetzen zersetzen zerstören Doch es darf nicht mir gehören Ich muss zerstören

      C 1 Reply Last reply
      0
      • J Jorgen Sigvardsson

        So.. uh, the system timer resolution is too low? Lucky me that I don't have to do anything multimedia related, because I was under the impression that a timer which is given the attribute system would be the most accurate one. Thanks for the heads up - who knows? I might have to deal with issues like this some day...

        -- Verletzen zerfetzen zersetzen zerstören Doch es darf nicht mir gehören Ich muss zerstören

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #20

        it's updated every ~15ms to 50ms (depending on the version of Windows). so, it is measuring time in the ms range, but the difference between two consecutive calls will never be less than ~15ms. QueryPerformanceCounter is much more accurate.

        image processing toolkits | batch image processing | blogging

        A 1 Reply Last reply
        0
        • K kasturi_haribabu

          1. Are you sure your function which is posting message takes '0'ms to execute ? I raise this question because, if your calling code has a lot to do and then sleep 10 ms, the interval between subsequent calls would be (Sleep time + Time taken to execute other part of the function). If you are very specific about the response time, i dont think post message is the right way to address such issuesbecause the latency of the response is not guaranted. You can use Events or a Semaphore-give-release technique to decrease the latency further. but the technique you need to choose depends on your applcation. Haribabu.

          A Offline
          A Offline
          acerunner316
          wrote on last edited by
          #21

          I don't know if it takes 0ms, but there is nothing else to my code other than repeatedly posting messages. UINT CNOx1000MonitorDlg::FThreadLoop (LPVOID PptrToClass) { CMainWindowDlg *LptrClass = static_cast(PptrToClass); HWND *ptrWindowHandle = new HWND; *ptrWindowHandle = LptrClass->GetSafeHwnd(); while(LptrClass->mbolThreadExecute) { ::PostMessage(*ptrWindowHandle, WM_UPDATE_CONTROLS, 0, 0); ::Sleep(10); } delete ptrWindowHandle; return 0; }

          1 Reply Last reply
          0
          • C Chris Losinger

            it's updated every ~15ms to 50ms (depending on the version of Windows). so, it is measuring time in the ms range, but the difference between two consecutive calls will never be less than ~15ms. QueryPerformanceCounter is much more accurate.

            image processing toolkits | batch image processing | blogging

            A Offline
            A Offline
            acerunner316
            wrote on last edited by
            #22

            How will I use QueryPerformanceCounter as a way of sampling data? I will still need a thread to loop a function call. And in that function will check QueryPerformanceCounter. But if posting a message takes longer than 6ms. Then QueryPerformanceCounter can only be as accurate as that. Can you provide a code sample? Im fairly new to windows programming. I am basically learning from sample code that I find online. Thanks.

            C 1 Reply Last reply
            0
            • A acerunner316

              How will I use QueryPerformanceCounter as a way of sampling data? I will still need a thread to loop a function call. And in that function will check QueryPerformanceCounter. But if posting a message takes longer than 6ms. Then QueryPerformanceCounter can only be as accurate as that. Can you provide a code sample? Im fairly new to windows programming. I am basically learning from sample code that I find online. Thanks.

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #23

              acerunner316 wrote:

              I will still need a thread to loop a function call. And in that function will check QueryPerformanceCounter.

              right

              acerunner316 wrote:

              But if posting a message takes longer than 6ms. Then QueryPerformanceCounter can only be as accurate as that.

              right maybe this article can help... scroll down to the bottom for a message-loop / QueryPerfCounter sample.

              image processing toolkits | batch image processing | blogging

              A 1 Reply Last reply
              0
              • C Chris Losinger

                acerunner316 wrote:

                I will still need a thread to loop a function call. And in that function will check QueryPerformanceCounter.

                right

                acerunner316 wrote:

                But if posting a message takes longer than 6ms. Then QueryPerformanceCounter can only be as accurate as that.

                right maybe this article can help... scroll down to the bottom for a message-loop / QueryPerfCounter sample.

                image processing toolkits | batch image processing | blogging

                A Offline
                A Offline
                acerunner316
                wrote on last edited by
                #24

                sorry, i'm still having trouble understanding. In code sample at the link you gave, I can see that the function is monitoring for any messages and checking for the time. But there is the message being generated from, and how?

                C 1 Reply Last reply
                0
                • A acerunner316

                  sorry, i'm still having trouble understanding. In code sample at the link you gave, I can see that the function is monitoring for any messages and checking for the time. But there is the message being generated from, and how?

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #25

                  acerunner316 wrote:

                  But there is the message being generated from, and how?

                  the message comes from code outside the loop. this is pretty close to an old-style C Windows message loop. Windows is passing messages (button down, repaint, keydown, etc) to the application, which the app grabs, when it sees one, and starts processing with that Peek/Translate/DispathMessage bit - other code is responsible for handling the specific msgs. otherwise, every 40ms, the app does a run through its Render/Move/etc stuff, then goes back to looking for messages.

                  image processing toolkits | batch image processing | blogging

                  A 1 Reply Last reply
                  0
                  • C Chris Losinger

                    acerunner316 wrote:

                    But there is the message being generated from, and how?

                    the message comes from code outside the loop. this is pretty close to an old-style C Windows message loop. Windows is passing messages (button down, repaint, keydown, etc) to the application, which the app grabs, when it sees one, and starts processing with that Peek/Translate/DispathMessage bit - other code is responsible for handling the specific msgs. otherwise, every 40ms, the app does a run through its Render/Move/etc stuff, then goes back to looking for messages.

                    image processing toolkits | batch image processing | blogging

                    A Offline
                    A Offline
                    acerunner316
                    wrote on last edited by
                    #26

                    Isn't this similar to what I'm already doing, posting messages every 10ms? Because what I'm doing is posting the message repeatedly in a loop every 10ms. And then I have a function mapped to handle that message ON_MESSAGE(WM_UPDATE_DATA, OnUpdateData). So the only difference I see is instead of using ON_MESSAGE, I will have my own function that loops to check for the message posted an respond to it. Is this right? Will this make the difference of the 6ms delay? Would I have to post this message like every 1ms in order to get the 1ms resolution?

                    C 1 Reply Last reply
                    0
                    • A acerunner316

                      Isn't this similar to what I'm already doing, posting messages every 10ms? Because what I'm doing is posting the message repeatedly in a loop every 10ms. And then I have a function mapped to handle that message ON_MESSAGE(WM_UPDATE_DATA, OnUpdateData). So the only difference I see is instead of using ON_MESSAGE, I will have my own function that loops to check for the message posted an respond to it. Is this right? Will this make the difference of the 6ms delay? Would I have to post this message like every 1ms in order to get the 1ms resolution?

                      C Offline
                      C Offline
                      Chris Losinger
                      wrote on last edited by
                      #27

                      acerunner316 wrote:

                      Isn't this similar to what I'm already doing, posting messages every 10ms?

                      pretty much. but Sleep simply isn't that accurate. Sleep uses the system clock, which has a resolution closer to 15ms per tick in most modern versions of Windows. and, it's entirely possible for your sleeping thread to get preempted by another thread, further delaying its awakening. i'm not sure what the overhead in PostMessage / ON_MESSAGE amounts to. but they're asynchronous anyway; apps respond to messages when they get around to it, not the instant they appear in their message. the time from PostMessage to the actual response is (i believe) indeterminate.

                      image processing toolkits | batch image processing | blogging

                      A 1 Reply Last reply
                      0
                      • C Chris Losinger

                        acerunner316 wrote:

                        Isn't this similar to what I'm already doing, posting messages every 10ms?

                        pretty much. but Sleep simply isn't that accurate. Sleep uses the system clock, which has a resolution closer to 15ms per tick in most modern versions of Windows. and, it's entirely possible for your sleeping thread to get preempted by another thread, further delaying its awakening. i'm not sure what the overhead in PostMessage / ON_MESSAGE amounts to. but they're asynchronous anyway; apps respond to messages when they get around to it, not the instant they appear in their message. the time from PostMessage to the actual response is (i believe) indeterminate.

                        image processing toolkits | batch image processing | blogging

                        A Offline
                        A Offline
                        acerunner316
                        wrote on last edited by
                        #28

                        Chris Losinger wrote:

                        pretty much. but Sleep simply isn't that accurate. Sleep uses the system clock, which has a resolution closer to 15ms per tick in most modern versions of Windows. and, it's entirely possible for your sleeping thread to get preempted by another thread, further delaying its awakening.

                        In that case, what do I use to loop posting of a message?

                        C 1 Reply Last reply
                        0
                        • A acerunner316

                          Chris Losinger wrote:

                          pretty much. but Sleep simply isn't that accurate. Sleep uses the system clock, which has a resolution closer to 15ms per tick in most modern versions of Windows. and, it's entirely possible for your sleeping thread to get preempted by another thread, further delaying its awakening.

                          In that case, what do I use to loop posting of a message?

                          C Offline
                          C Offline
                          Chris Losinger
                          wrote on last edited by
                          #29

                          honestly, i don't know. Windows really isn't a real-time OS, and as far as I know, there's not a lot of high-level support for the kind of timing precision you seem to want. maybe you could check an open source game or audio application (ex. Audacity) that you could plunder for ideas.

                          image processing toolkits | batch image processing | blogging

                          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