c++ - c ++ usleep在linux上立即返回
问题描述
简单用法:
#include <unistd.h>
#include <iostream>
int main()
{
std::cout << usleep(20 * 1000) << std::endl;
return 0;
}
编译g++ main.cpp
。可执行退出立即打印0
指示未检测到错误。那么有什么问题呢?
解决方案
实际上,您传递给 usleep() 的参数以微秒为单位。所以在 20 毫秒内程序退出..您可以传递 20 *1000000 或者您可以使用 chrono 库。
#include <iostream> // std::cout, std::endl
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
int main()
{
std::cout << "countdown:\n";
for (int i=10; i>0; --i) {
std::cout << i << std::endl;
std::this_thread::sleep_for (std::chrono::seconds(1));
}
std::cout << "Lift off!\n";
return 0;
}
推荐阅读
- android - React Native - Android - 如何将活动转换为透明?
- hellosign-api - HelloWorks - 如何将显示文本组件添加到条件规则组件
- asp.net-mvc - 从 TFS 中的发布/发布中排除上次签入
- swift - 我的返回值为 nil,我不知道为什么(Swift)
- google-sheets - 条件格式,单元格引用移位
- python - 如何为文档相似度创建一个新的余弦相似度函数
- c++ - 尝试创建一个以表格格式传递二维数组输出数组的函数(类似于 python3 中的制表)
- javascript - 在javascript的后台使用post方法打开url
- javascript - Digital Ocean Spaces:无法访问 text/css 和 text/javascript mime 类型文件
- c++ - 使用联合为结构创建构造函数