Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

c++ - 睡眠数毫秒(Sleep for milliseconds)

I know the POSIX sleep(x) function makes the program sleep for x seconds.

(我知道POSIX sleep(x)函数使程序休眠x秒。)

Is there a function to make the program sleep for x milliseconds in C++?

(在C ++中是否有使程序休眠x 毫秒的功能?)

  ask by Prasanth Madhavan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In C++11, you can do this with standard library facilities:

(在C ++ 11中,可以使用标准库工具来执行此操作:)

#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));

Clear and readable, no more need to guess at what units the sleep() function takes.

(清晰易读,无需再猜测sleep()函数采用的单位。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...