C++ delay

MS VC++可以用MFC的Sleep函数,参数是毫秒。

delay函数要自己写,编译器里没有。

#include <time.h> // 头文件

time_t start_time, cur_time; // 变量声明

time(&start_time);
do { time(&cur_time);
} while((cur_time - start_time) < 3);

上面的 3 是 迟后 3 秒

很容易改写成自己的delay函数:

#include <time.h>
void delay(int sec)
{
time_t start_time, cur_time; // 变量声明
time(&start_time);
do { time(&cur_time);
} while((cur_time - start_time) < sec );
}

调用:
(void) delay(5); // 滞后5秒

短于一秒的delay可以这样写:
clock_t start_time, cur_time;
start_time = clock();
while((clock() - start_time) < 3.0 * CLOCKS_PER_SEC)
{
}
但有的编译器不支持clock

推荐MS VC++ MFC的Sleep(毫秒)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值