Walter
Walter

Reputation: 1470

C++: More precise timing intervals

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

The task needs to be run every second or every 10 seconds.

Is there a better way to do this than:

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

Walter

Upvotes: 2

Views: 432

Answers (2)

Dat Chu
Dat Chu

Reputation: 11140

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.

Upvotes: 0

kevin
kevin

Reputation: 2222

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

Upvotes: 1

Related Questions