10 秒ごとにループをロードし、カウントに +1 を追加して出力する方法は?
お気に入り:
int count;
while(true)
{
count +=1;
cout << count << endl; // print every 10 second
}
印刷:
1
2
3
4
5
ect...
方法がわかりません、助けてください
10 秒ごとにループをロードし、カウントに +1 を追加して出力する方法は?
お気に入り:
int count;
while(true)
{
count +=1;
cout << count << endl; // print every 10 second
}
印刷:
1
2
3
4
5
ect...
方法がわかりません、助けてください
私の試み。(ほぼ) 完全に 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;
}
Sleep
Windows では、以下から使用できます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);
}
}
Windows Waitable Timerを使用したさらに別の例を次に示します。MSDN ページからの短い引用:
待機可能なタイマーオブジェクトは、指定された期限が来ると状態がシグナル状態に設定される同期オブジェクトです。作成できる待機可能なタイマーには、手動リセットと同期の 2 種類があります。どちらのタイプのタイマーも定期的なタイマーにすることができます。
例:
#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;
}
}
}
また、 Asynchronous Procedure CallでWaitable Timerを使用することもできます。MSDN でこの例を参照してください。
使用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);
}
他の人が言ったように、sleep() 関数が必要です。ただし、クロスプラットフォームの問題が発生する可能性があります。その問題についてこのスレッドを見つけました。あなたが見たいと思うかもしれません。