Search code examples
c++functiontimecall

Deferred function call in C/C++


How can I call a function in C++ after a certain period of time or at a particular time?
I searched in Google and in Stackoverflow. I only found way to do this through SIGALARM handler.

Update 1:
P.S. I use Linux.
P.P.S. I haven't got any written code, because I want to know how to do that, before writing.<


Solution

  • You'd probably want to do that in another throwaway thread, as waiting in the main thread would block your app. You can add the delay in that thread by using std::this_thread::sleep_for.

    I.e.

    using namespace std;
    thread([]{this_thread::sleep_for(chrono::milliseconds(1000)); foo(); }).detach();