HOW TO SLEEP FOR IN C++

在使用C++11的时候,我需要对线程进行等待操作,下面有四种方法: 

  1. Use usleep (POSIX)
  2. Use Sleep (Windows)
  3. Use Boost.Thread
  4. Use std::this_thread::sleep_for (C++11)

Method 1: Use usleep (POSIX)

1

2

3

4

5

6

#include

 

int main()

{

    usleep(500);

}

Method 2: Use Sleep (Windows)

1

2

3

4

5

6

#include

 

int main()

{

    Sleep(500);

}

Method 3: Use Boost.Thread

1

2

3

4

5

#include

int main()

{

    boost::this_thread::sleep(boost::posix_time::milliseconds(500));

}

Method 4: Use std::this_thread::sleep_for (C++11)

1

2

3

4

5

6

7

#include

#include

 

int main()

{

    std::this_thread::sleep_for(std::chrono::milliseconds(500));

}

 

参考文章:

http://www.martinbroadhurst.com/sleep-for-milliseconds-in-c.html

https://stackoverflow.com/questions/2252372/how-do-you-make-a-program-sleep-in-c-on-win-32

你可能感兴趣的:(c++书籍)