2

如何每 10 秒加载一个循环并将 +1 添加到计数并打印它?

像:

int count;
    while(true)
    {
       count +=1;
       cout << count << endl; // print every 10 second 
    }

打印:

1
2
3
4
5
ect...

我不知道怎么做,请帮帮我

4

6 回答 6

11

我的尝试。(几乎)完美的 POSIX。也适用于 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 = %ld\n", 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("%d\n", count);
            count++;
        }

        printf("DebugTime = %f\n", 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("%d\n", count++);
    }
    return 0;
}
于 2012-05-29T22:48:07.447 回答
4

在 Windows 上,您可以使用Sleepfrom windows.h

#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);
    }
}
于 2012-05-29T22:40:47.633 回答
2

使用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);

}
于 2012-05-29T22:36:53.113 回答
2

这是另一个使用 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;
        }
    }
}

此外,您可以将Waitable Timer异步过程调用一起使用。请参阅MSDN 上的示例。

于 2012-06-06T08:30:01.013 回答
1

正如其他人所说,您需要一个 sleep() 函数。但是可能会出现跨平台问题。我找到了关于这个问题的这个帖子,你可能想看看。

于 2012-05-29T22:41:43.457 回答
0

在 C++11(或 14)及更高版本中,您还获得了chrono带有标准库的库,因此您可以调用它,这对于多线程也非常有用:

#include <chrono>

for (;;)
{
  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  if (someEndCondition)
    break;
}
于 2020-07-07T22:37:20.153 回答