implement time delay in c

后端 未结 17 1916
我在风中等你
我在风中等你 2020-11-30 05:43

I don\'t know exactly how to word a search for this.. so I haven\'t had any luck finding anything.. :S

I need to implement a time delay in C.

for example I w

17条回答
  •  盖世英雄少女心
    2020-11-30 06:16

    C11 has a function specifically for this:

    #include 
    #include 
    #include 
    
    void sleep(time_t seconds) {
        struct timespec time;
        time.tv_sec = seconds;
        time.tv_nsec = 0;
        while (thrd_sleep(&time, &time)) {}
    }
    
    int main() {
        puts("Sleeping for 5 seconds...");
        sleep(5);
        puts("Done!");
        return 0;
    }
    

    Note that this is only available starting in glibc 2.28.

提交回复
热议问题