#include <iostream>
#include <thread> // 引入线程库,包含sleep函数
#include <chrono> // 引入时间库,用于指定休眠的时间单位
int main() {
std::cout << "程序开始...\n";
// 使用std::this_thread::sleep_for进行休眠
// 参数为std::chrono::seconds(5),表示休眠5秒
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "5秒后程序继续执行。\n";
return 0;
}
<thread> 和 <chrono> 是C++11标准库中提供的两个头文件。<thread> 提供了多线程支持,而 <chrono> 提供了高精度的时间操作。std::this_thread::sleep_for:这是C++11中推荐的休眠方法,它可以让当前线程暂停指定的时间。这里的参数是 std::chrono::seconds(5),表示让线程暂停5秒。如果你需要更长时间或不同单位的休眠,可以调整 std::chrono::seconds(5) 中的参数,例如使用 std::chrono::milliseconds(500) 表示500毫秒。
上一篇:c++ set find
下一篇:c++变量
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站