C++: More precise timing intervals
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.
精彩评论