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

后端 未结 9 1000
感动是毒
感动是毒 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: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 */
       }
    }
    

提交回复
热议问题