sleep

There exists a POSIX C API that can be used to put the program execution on hold for a specified duration. This is done via the sleep command:

#include <unistd.h>
#include <iostream>

int main() {
    unsigned int seconds = 10;
    std::cout << "Waiting for 10 seconds...";
    sleep(seconds);
    std::cout << "Done." << std::endl;
    return 0;
}

In C++11, this can be accomplished via the API provided from the thread library:

#include <thread>
#include <chrono>
#include <iostream>

int main() {
    std::cout << "Waiting for 10 seconds...";
    std::this_thread::sleep_for(std::chrono::seconds(10));
    std::cout << "Done." << std::endl;
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *