每10秒循环一次

时间:2012-05-29 22:34:34

标签: c++

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

像:

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

打印:

1
2
3
4
5
ect...

我不知道怎么样,请帮帮我们

6 个答案:

答案 0 :(得分:8)

我的尝试。 (几乎)完美的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;
}

答案 1 :(得分:3)

在Windows上,您可以使用Sleep中的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);
    }
}

答案 2 :(得分: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);

}

答案 3 :(得分:1)

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

答案 4 :(得分:1)

以下是使用Windows Waitable Timer的另一个示例。来自MSDN页面的简短引用:

  

waitable timer 对象是一个同步对象,其状态设置为在指定的到期时间到达时发出信号。可以创建两种类型的可等待计时器:手动重置和同步。任何一种类型的计时器也可以是周期性计时器

示例:

#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上的this示例。

答案 5 :(得分:0)

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

#include <chrono>

for (;;)
{
  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  if (someEndCondition)
    break;
}
相关问题