MATH000
MATH000

Reputation: 1103

Run a function at the start of every minute?

I am trying to run a function at the start of every minute mm:00.000

So, I want to run a function (perfomance is very importnant) every time this condition is true:

(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) % 6000 == 0

Any idea how to do this?

Upvotes: 1

Views: 385

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118340

Start a separate thread.

The thread checks std::chrono_system_clock, and computes the absolute time for the next minute boundary. There are several ways to make the thread sleep until the prescribed time arrives. One way would be for thread to create a private mutex and condition variable, lock the mutex, and call wait_until() the absolute time for the next minute boundary. Since nothing else will notify the condition variable, the thread will simply sleep until the prescribed time arrives, then your thread can invoke the given function.

A separate thread is strictly not necessary. This could all be done as part of your main execution thread, if your main execution thread has nothing to do, otherwise.

Upvotes: 1

Related Questions