我知道POSIX休眠(x)函数使程序休眠x秒。C++中是否有一个函数可以让程序休眠x毫秒?
当前回答
注意,没有毫秒的标准C API,因此(在Unix上)您必须接受usleep,它接受微秒:
#include <unistd.h>
unsigned int microseconds;
...
usleep(microseconds);
其他回答
为了保持便携性,您可以使用Boost::Thread进行睡眠:
#include <boost/thread/thread.hpp>
int main()
{
//waits 2 seconds
boost::this_thread::sleep( boost::posix_time::seconds(1) );
boost::this_thread::sleep( boost::posix_time::milliseconds(1000) );
return 0;
}
此答案是重复的,以前已在该问题中发布过。也许你也可以在那里找到一些有用的答案。
注意,没有毫秒的标准C API,因此(在Unix上)您必须接受usleep,它接受微秒:
#include <unistd.h>
unsigned int microseconds;
...
usleep(microseconds);
如果使用MS Visual C++10.0,则可以使用标准库工具执行此操作:
Concurrency::wait(milliseconds);
您需要:
#include <concrt.h>
在C++中休眠程序的方法是sleep(int)方法。它的头文件是#include“windows.h”
例如:
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int main()
{
int x = 6000;
Sleep(x);
cout << "6 seconds have passed" << endl;
return 0;
}
它的睡眠时间以毫秒为单位,没有限制。
Second = 1000 milliseconds
Minute = 60000 milliseconds
Hour = 3600000 milliseconds
我使用这个:
#include <thread>
#define sleepms(val) std::this_thread::sleep_for(val##ms)
例子:
sleepms(200);
推荐文章
- decltype(auto)的一些用途是什么?
- Shared_ptr转换为数组:应该使用它吗?
- Printf与std::字符串?
- 在Bash中检查变量是否存在于列表中
- JavaScript中有睡眠/暂停/等待功能吗?
- 禁用复制构造函数
- 查看PS命令的全部输出
- 只接受特定类型的c++模板
- c#和Java中的泛型有什么不同?和模板在c++ ?
- Linux命令将域名转换为IP
- c++ 11中的递归lambda函数
- 在c++中指针使用NULL或0(零)吗?
- 在c++中,如何将int值附加到字符串中?
- 如何从命令行在windows中找到mysql数据目录
- 就性能而言,使用std::memcpy()还是std::copy()更好?