开发者

C++: More precise timing intervals

开发者 https://www.devze.com 2023-02-06 00:39 出处:网络
I need to periodically do a particular task and am currently using nanosleep. The task needs to be ru开发者_Go百科n every second or every 10 seconds.

I need to periodically do a particular task and am currently using nanosleep.

The task needs to be ru开发者_Go百科n every second or every 10 seconds.

Is there a better way to do this than:

while(true)
{
 doTask();
 sleep();
}

Walter


One of the options could be to create a thread that will do the task with specified timeout.


You can use a thread library to create a thread which handle run the doTask(). Your main thread just keeps sleeping and runs every 1 second or 10 seconds.

This can be done with a QTimer and a QRunnable.

http://doc.qt.nokia.com/latest/qtimer.html

According to the dock, the resolution is around 1 ms in most cases. For your need, this should be sufficient.

0

精彩评论

暂无评论...
验证码 换一张
取 消