How to run a function at a specific time in C ++?

Is it possible to call a function at a specific time in C ++? For example, I would like to run the doIt () function when number_of_elapsed_milliseconds_since_application_start = x.

A cross platform solution would be ideal.

+6
source share
3 answers

In pure C ++, probably not, you will need certain OS code. But you can use a platform-independent OS shell such as Qt (although this may be a little redundant for your rather simple problem).

EDIT: The simplest thing you could do is actively block the program in a loop that constantly checks the current time until the deadline is reached, but this is probably not a very useful solution, Thus, without threads or some sort of controlled timer events (like every OS) you won't get very far.

+3
source

Create a thread, put it to sleep before this time, and after sleep, run this function.

+3
source

This is what is considered a CallBack function or β€œlistener”. More information about this can be found here: http://www.cprogramming.com/tutorial/function-pointers.html

Hope this helps.

+1
source

Source: https://habr.com/ru/post/888729/


All Articles