Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I launch a function at a specific time in C++?

Tags:

c++

time

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

A cross-platform solution would be ideal.

like image 786
Shawn Avatar asked May 21 '11 22:05

Shawn


2 Answers

In pure C++ probably not, you will need some OS specific code. But you can use a platform-independent OS-wrapper, like Qt (although this could be a bit of an overkill for your quite simple problem).

EDIT: The simplest thing you could do, is actively blocking the program in a loop, that constantly polls the current time, until the deadline is reached, but that is probably not a very useful solution. So without threads or some event-driven timer (as every OS should have) you won't get very far.

like image 70
Christian Rau Avatar answered Oct 05 '22 03:10

Christian Rau


Make a thread, put it to sleep until that time, and after the sleep, have it run that function.

like image 23
George Kastrinis Avatar answered Oct 05 '22 01:10

George Kastrinis