C++线程休眠
一、直接线程休眠函数(最常用)
1. std::this_thread::sleep_for – 休眠一个时长
#include <thread> #include <chrono> std::this_thread::sleep_for(std::chrono::milliseconds(500));
这个需要引入两个头文件
-
参数是一个
std::chrono::duration对象,表示要休眠的时长。std::this_thread::sleep_for(std::chrono::seconds(2)); // second【秒】 模拟耗时操作 std::this_thread::sleep_for(std::chrono::microseconds(2)); // microseconds 【毫秒】 模拟耗时操作
二。
std::this_thread::sleep_for + std::chrono_literals(最简洁的现代写法)
using namespace std::chrono_literals; std::this_thread::sleep_for(500ms); //休眠500ms std::this_thread::sleep_for(5s);//休眠5s

浙公网安备 33010602011771号