macOS sleep操作 c++

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/glw0223/article/details/89371658

macOS sleep操作 c++

C++03:

Since Mac OS X is Unix-based, you can almost always just use the standard linux functions!

In this case you can use usleep (which takes a time in microseconds) and just multiply your milliseconds by 1000 to get microseconds.

#include <unistd.h>
int main () {
    usleep(1000); // will sleep for 1 ms
    usleep(1); // will sleep for 0.001 ms
    usleep(1000000); // will sleep for 1 s
}
c++11

If you have C++11 support in your compiler, you can use the sleep_for and avoid having to use an OS specific API. (http://en.cppreference.com/w/cpp/thread/sleep_for)

#include <thread>
#include <chrono>

int main()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    return 0;
}

参考:https://stackoverflow.com/questions/19439672/sleep-operation-in-c-on-os-x

展开阅读全文

没有更多推荐了,返回首页