每 10 秒循环一次
Loop every 10 second
如何每 10 秒加载一次循环并将 +1 添加到计数中并打印出来?
喜欢:
int count;
while(true)
{
count +=1;
cout << count << endl; // print every 10 second
}
打印:
1
2
3
4
5
ect...
我不知道怎么做,请帮帮我
我的尝试。(几乎)完美。也适用于POSIX和MSVC/Win32。
#include <stdio.h>
#include <time.h>
const int NUM_SECONDS = 10;
int main()
{
int count = 1;
double time_counter = 0;
clock_t this_time = clock();
clock_t last_time = this_time;
printf("Gran = %ldn", NUM_SECONDS * CLOCKS_PER_SEC);
while(true)
{
this_time = clock();
time_counter += (double)(this_time - last_time);
last_time = this_time;
if(time_counter > (double)(NUM_SECONDS * CLOCKS_PER_SEC))
{
time_counter -= (double)(NUM_SECONDS * CLOCKS_PER_SEC);
printf("%dn", count);
count++;
}
printf("DebugTime = %fn", time_counter);
}
return 0;
}
这样,您还可以控制每次迭代,这与基于 sleep() 的方法不同。
该解决方案(或基于高精度定时器的解决方案)还可确保计时中没有误差累积。
编辑:OSX的东西,如果所有其他方法都失败了
#include <unistd.h>
#include <stdio.h>
const int NUM_SECONDS = 10;
int main()
{
int i;
int count = 1;
for(;;)
{
// delay for 10 seconds
for(i = 0 ; i < NUM_SECONDS ; i++) { usleep(1000 * 1000); }
// print
printf("%dn", count++);
}
return 0;
}
在 Windows 上,您可以使用 windows.h
中的Sleep
:
#include <iostream>
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
int count = 0;
while(true)
{
count +=1;
std::cout << count << std::endl;
Sleep(10000);
}
}
使用 sleep()
: http://www.gnu.org/software/libc/manual/html_node/Sleeping.html
int count = 0;
while(true)
{
count++;
cout << count << endl; // print every 10 second
sleep(10);
}
这是另一个使用 Windows Waitable Timer 的示例。MSDN 页面的简短引用:
可等待计时器对象是一个同步对象,其状态设置为在指定的到期时间到达时发出信号。可以创建两种类型的可等待计时器:手动重置和同步。任一类型的计时器也可以是周期计时器。
例:
#include <Windows.h>
#include <iostream>
void main(int argc, char** argv)
{
HANDLE hTimer = NULL;
LARGE_INTEGER liTimeout;
// Set timeout to 10 seconds
liTimeout.QuadPart = -100000000LL;
// Creating a Waitable Timer
hTimer = CreateWaitableTimer(NULL,
TRUE, // Manual-reset
"Ten-Sec Timer" // Timer's name
);
if (NULL == hTimer)
{
std::cout << "CreateWaitableTimer failed: " << GetLastError() << std::endl;
return;
}
// Initial setting a timer
if (!SetWaitableTimer(hTimer, &liTimeout, 0, NULL, NULL, 0))
{
std::cout << "SetWaitableTimer failed: " << GetLastError() << std::endl;
return;
}
std::cout << "Starting 10 seconds loop" << std::endl;
INT16 count = 0;
while (count < SHRT_MAX)
{
// Wait for a timer
if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
{
std::cout << "WaitForSingleObject failed: " << GetLastError() << std::endl;
return;
}
else
{
// Here your code goes
std::cout << count++ << std::endl;
}
// Set a timer again
if (!SetWaitableTimer(hTimer, &liTimeout, 0, NULL, NULL, 0))
{
std::cout << "SetWaitableTimer failed: " << GetLastError() << std::endl;
return;
}
}
}
此外,还可以将可等待计时器与异步过程调用一起使用。请参阅 MSDN 上的此示例。
正如其他人所说,你想要一个sleep()函数。不过,可能会出现跨平台问题。我找到了关于该问题的线程,您可能想查看。
在 C++11(或 14)及更高版本中,您还可以获得带有标准 lib 的 chrono
库,因此您可以调用它,这对于多线程也非常有用:
#include <chrono>
for (;;)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (someEndCondition)
break;
}
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(10));
std::cout << i << std::endl;
}
}
相关文章:
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- (C++)虽然循环一次不起作用,但我引入了多个变量
- 花括号使循环运行一次?
- 从子菜单返回后,正确的输入至少进入验证循环一次
- foor 循环的最后一次迭代中的指针更改
- 有没有办法在循环中的条件至少满足一次时运行语句?
- 循环中本地对象的析构函数是否保证在下一次迭代之前被调用?
- For循环在cpp中仅执行一次
- 如何强制循环执行另一次迭代到零并关闭循环
- (C++)for循环中的空初始语句是否保存所有编译器中变量的上一次初始化
- 为什么在C/C++中的while循环中只发生一次取消引用
- 如何防止我的循环额外运行一次?
- Atmel Studio 7:ASF 库提供的 USART 中断处理程序仅在 main() 中遇到无限循环时工作一次?
- 如何在主游戏循环中使用 sfml 只播放一次声音
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- C++,查找最低值,函数错误:循环最多运行一次(循环增量从未执行)
- boost::multi_index_container:从任意索引中取equal_range,只实现一次循环
- 使用sleep_for每毫秒运行一次循环
- 循环将最多运行一次(循环增量永远不会执行)
- 为什么函数立即完成一次循环,然后再次运行