How do you add a timed delay to a C++ program?

后端 未结 14 2103
情话喂你
情话喂你 2020-11-28 02:11

I am trying to add a timed delay in a C++ program, and was wondering if anyone has any suggestions on what I can try or information I can look at?

I wish I had more

相关标签:
14条回答
  • 2020-11-28 02:28

    The top answer here seems to be an OS dependent answer; for a more portable solution you can write up a quick sleep function using the ctime header file (although this may be a poor implementation on my part).

    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    void sleep(float seconds){
        clock_t startClock = clock();
        float secondsAhead = seconds * CLOCKS_PER_SEC;
        // do nothing until the elapsed time has passed.
        while(clock() < startClock+secondsAhead);
        return;
    }
    int main(){
    
        cout << "Next string coming up in one second!" << endl;
        sleep(1.0);
        cout << "Hey, what did I miss?" << endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-28 02:30

    Note that this does not guarantee that the amount of time the thread sleeps will be anywhere close to the sleep period, it only guarantees that the amount of time before the thread continues execution will be at least the desired amount. The actual delay will vary depending on circumstances (especially load on the machine in question) and may be orders of magnitude higher than the desired sleep time.

    Also, you don't list why you need to sleep but you should generally avoid using delays as a method of synchronization.

    0 讨论(0)
  • 2020-11-28 02:33

    You can also use select(2) if you want microsecond precision (this works on platform that don't have usleep(3))

    The following code will wait for 1.5 second:

    #include <sys/select.h>
    #include <sys/time.h>
    #include <unistd.h>`
    
    int main() {
        struct timeval t;
        t.tv_sec = 1;
        t.tv_usec = 500000;
        select(0, NULL, NULL, NULL, &t);
    }
    

    `

    0 讨论(0)
  • 2020-11-28 02:34

    In Win32:

    #include<windows.h>
    Sleep(milliseconds);
    

    In Unix:

    #include<unistd.h>
    unsigned int microsecond = 1000000;
    usleep(3 * microsecond);//sleeps for 3 second
    

    sleep() only takes a number of seconds which is often too long.

    0 讨论(0)
  • 2020-11-28 02:35

    Yes, sleep is probably the function of choice here. Note that the time passed into the function is the smallest amount of time the calling thread will be inactive. So for example if you call sleep with 5 seconds, you're guaranteed your thread will be sleeping for at least 5 seconds. Could be 6, or 8 or 50, depending on what the OS is doing. (During optimal OS execution, this will be very close to 5.)
    Another useful feature of the sleep function is to pass in 0. This will force a context switch from your thread.

    Some additional information:
    http://www.opengroup.org/onlinepubs/000095399/functions/sleep.html

    0 讨论(0)
  • 2020-11-28 02:40

    Do you want something as simple like:

    #include <unistd.h>
    sleep(3);//sleeps for 3 second
    
    0 讨论(0)
提交回复
热议问题