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

后端 未结 9 981
感动是毒
感动是毒 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:12

    While this does not answer the question, it might give another look at the solution. What about placing the simulation code and user interface in different threads? If you use Qt, periodic update can be realized using a timer or even QThread::msleep(). You can adapt the threaded Mandelbrot example to suit your need.

    0 讨论(0)
  • 2020-12-11 14:15

    You might consider just updating the viewport every N simulation steps rather than every K milliseconds. If this is (say) a serious commercial app, then you're probably going to want to go the multi-thread route suggested elsewhere, but if (say) it's for personal or limited-audience use and what you're really interested in is the details of whatever it is you're simulating, then every-N-steps is simple, portable and may well be good enough to be getting on with.

    0 讨论(0)
  • 2020-12-11 14:20

    The code snippet example in this link pretty much does what you want:

    http://www.cplusplus.com/reference/clibrary/ctime/clock/

    Adapted from their example:

    void runwait ( int seconds )
    {
       clock_t endwait;
       endwait = clock () + seconds * CLOCKS_PER_SEC ;
       while (clock() < endwait)
       {
          /* Do stuff while waiting */
       }
    }
    
    0 讨论(0)
提交回复
热议问题