Sleeping for milliseconds on Windows, Linux, Solaris, HP-UX, IBM AIX, Vxworks, Wind River Linux?

后端 未结 3 998
深忆病人
深忆病人 2020-12-10 13:02

I have to write a C program which has to sleep for milliseconds, which has to run on various platforms like Windows, Linux, Solaris, HP-UX, IBM AIX, Vxworks, and Windriver L

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 13:43

    I note that usleep is obsolescent but its a lot simpler than nanosleep. So I used it when I needed an enhanced sleep that would allow easy adjustment from seconds while debugging my scripts to milliseconds or zero for production.

    This snooze function combines the advantages of sleep & usleep so that you can enter an int or float for your desired delay and 0.1 will sleep a 10th of a second while 3 will sleep for 3 seconds. 3.5 seconds is treated as 3 seconds.

    Tested on Linux Mint 18.3 (Ubuntu 16.04.9) as C and C++ with gcc 5.4.0.

    #include 
    
    void snooze(double t) {(t > 1.0) ? sleep(t) : usleep(t*1000000);}
    
    snooze(0.01);  // call function to sleep for 10ms
    



    For completeness, this is a nanosleep version. It's potentially more accurate than the usleep version and isn't threatened by obsolescence.

    #include 
    #include 
    
    void snooze(double t) {
        struct timespec req = {t, fmod(t, 1.0) * 1E9};
        nanosleep(&req, NULL);
    }
    
    // struct timespec req = {t, fmod(t, 1.0) * 1E9};
    //  is equivalent to:
    // struct timespec req = {0};
    // req.tv_sec = t;
    // req.tv_nsec = fmod(t, 1.0) * 1000000000L;
    
    // NULL as value for *rem so no resumption after signal interrupts
    
    snooze(1.99);  // call for delay of 1.99 seconds
    

    As suggested by @alk, the following versions return the called sleep function's error should one occur or 0 if successful. Defining the structure rem(aining) also permits resumption after a signal interrupt.

    int snooze(double t) {
        return (t > 1.0) ? sleep(t) : usleep(t*1000000);
    }
    
    int snooze(double t) {
        struct timespec req = {t, fmod(t, 1.0) * 1E9};
        struct timespec rem = {0, 0.0};
        return nanosleep(&req, &rem);
    }
    

提交回复
热议问题