2012-05-29 101 views
3

如何每10秒加載一次循環並將計數值加1並打印出來?每10秒循環

喜歡:

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

打印:

1 
2 
3 
4 
5 
ect... 

我不知道怎麼了,請幫我出球員

+0

看看睡眠功能.. –

+0

你需要這樣的精確度是多少?如果粒度是秒,「睡眠」就是你的答案。如果您需要更大的粒度,您將需要搜索其他解決方案。 –

回答

7

我的嘗試。 (幾乎)完美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; 
} 
+0

嘗試將time_t更改爲clock_t。順便說一句,「無」意味着「它甚至不編譯」?或者它沒有按預期工作? –

+0

它只是沒有打印任何東西 – user1417815

+0

我將類型更改爲'clock_t'。這是我無需打開我的Mac即可完成的最佳選擇。 –

1

使用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爲int count;所以計數可以是任何事情。你的std :: cout << count會打印一些增加1的垃圾。 – ScarletAmaranth

+0

確實爲Windows工作?我認爲這只是一個Linux系統調用 – WindowsMaker

+1

@zaftcoAgeiha:[works on windows](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v = vs.85).aspx),只是一個_slightly_不同的名稱和參數類型。 –

3

在Windows上,您可以使用Sleepwindows.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); 
    } 
} 
+0

計數不工作 – user1417815

+0

你能提供更多關於這方面的信息嗎?它適用於我,VC++ 2008. –

1

正如其他人所說,你要睡眠()函數。跨平臺的問題可能會出現。我發現this thread關於這個問題,你可能想看看。

1

下面是使用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上的this示例。