In javascript there's this sweet, sweet function window.setTimeout( func, 1000 ) ;
which will asynchronously invoke func
after 1000 ms.
I want to do something similar in C++ (without multithreading), so I put together a sample loop like:
#include <stdio.h> struct Callback { // The _time_ this function will be executed. double execTime ; // The function to execute after execTime has passed void* func ; } ; // Sample function to execute void go() { puts( "GO" ) ; } // Global program-wide sense of time double time ; int main() { // start the timer time = 0 ; // Make a sample callback Callback c1 ; c1.execTime = 10000 ; c1.func = go ; while( 1 ) { // its time to execute it if( time > c1.execTime ) { c1.func ; // !! doesn't work! } time++; } }
How can I make something like this work?