How to schedule the execution of a function in 5 seconds with C++

1 Answer

0 votes
#include <iostream>
#include <thread>
#include <chrono>

// Function to be executed
void myFunction() {
    std::cout << "Function executed after 5 seconds!" << std::endl;
}

int main() {
    std::cout << "Waiting for 5 seconds..." << std::endl;

    // Pause the thread for 30 seconds
    std::this_thread::sleep_for(std::chrono::seconds(5));

    // Execute the function
    myFunction();
}

 
 
/*
run:
 
Waiting for 5 seconds...
Function executed after 5 seconds!
 
*/

 



answered Jun 23, 2025 by avibootz

Related questions

1 answer 121 views
121 views asked Apr 29, 2025 by avibootz
2 answers 118 views
118 views asked Apr 29, 2025 by avibootz
...