每 10 秒循环一次

Loop every 10 second

本文关键字:一次 循环      更新时间:2023-10-16

如何每 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;
     } 
}