What is the best way to exit out of a loop after an elapsed time of 30ms in C++

后端 未结 9 1006
感动是毒
感动是毒 2020-12-11 13:24

What is the best way to exit out of a loop as close to 30ms as possible in C++. Polling boost:microsec_clock ? Polling QTime ? Something else?

Something like:

9条回答
  •  死守一世寂寞
    2020-12-11 14:04

    According to your question, every 30ms you'd like to update the viewport. I wrote a similar app once that probed hardware every 500ms for similar stuff. While this doesn't directly answer your question, I have the following followups:

    • Are you sure that Blah(), for updating the viewport, can execute in less than 30ms in every instance?
    • Seems more like running Blah() would be done better by a timer callback.
    • It's very hard to find a library timer object that will push on a 30ms interval to do updates in a graphical framework. On Windows XP I found that the standard Win32 API timer that pushes window messages upon timer interval expiration, even on a 2GHz P4, couldn't do updates any faster than a 300ms interval, no matter how low I set the timing interval to on the timer. While there were high performance timers available in the Win32 API, they have many restrictions, namely, that you can't do any IPC (like update UI widgets) in a loop like the one you cited above.
    • Basically, the upshot is you have to plan very carefully how you want to have updates occur. You may need to use threads, and look at how you want to update the viewport.

    Just some things to think about. They caught me by surprise when I worked on my project. If you've thought these things through already, please disregard my answer :0).

提交回复
热议问题