Cでタイマーを使う方法は?私は仕事のために500ミリ秒まで待つ必要があります。この仕事をする良い方法を挙げてください。私はsleep(3);
を使用しましたが、この方法ではその期間内には何もしません。私はその時間までに何か入力を得ることを試みる何かを持っています。Cでタイマーを使うには?
5
A
答えて
4
4
は(それが#include <time.h>
を必要とする)私が使用ソリューションです:
int msec = 0, trigger = 10; /* 10ms */
clock_t before = clock();
do {
/*
* Do something to busy the CPU just here while you drink a coffee
* Be sure this code will not take more than `trigger` ms
*/
clock_t difference = clock() - before;
msec = difference * 1000/CLOCKS_PER_SEC;
iterations++;
} while (msec < trigger);
printf("Time taken %d seconds %d milliseconds (%d iterations)\n",
msec/1000, msec%1000, iterations);
0
この例では、あなたに役立つことかもしれ
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/*
Implementation simple timeout
Input: count milliseconds as number
Usage:
setTimeout(1000) - timeout on 1 second
setTimeout(10100) - timeout on 10 seconds and 100 milliseconds
*/
void setTimeout(int milliseconds)
{
// If milliseconds is less or equal to 0
// will be simple return from function without throw error
if (milliseconds <= 0) {
fprintf(stderr, "Count milliseconds for timeout is less or equal to 0\n");
return;
}
// a current time of milliseconds
int milliseconds_since = clock() * 1000/CLOCKS_PER_SEC;
// needed count milliseconds of return from this timeout
int end = milliseconds_since + milliseconds;
// wait while until needed time comes
do {
milliseconds_since = clock() * 1000/CLOCKS_PER_SEC;
} while (milliseconds_since <= end);
}
int main()
{
// input from user for time of delay in seconds
int delay;
printf("Enter delay: ");
scanf("%d", &delay);
// counter downtime for run a rocket while the delay with more 0
do {
// erase the previous line and display remain of the delay
printf("\033[ATime left for run rocket: %d\n", delay);
// a timeout for display
setTimeout(1000);
// decrease the delay to 1
delay--;
} while (delay >= 0);
// a string for display rocket
char rocket[3] = "-->";
// a string for display all trace of the rocket and the rocket itself
char *rocket_trace = (char *) malloc(100 * sizeof(char));
// display trace of the rocket from a start to the end
int i;
char passed_way[100] = "";
for (i = 0; i <= 50; i++) {
setTimeout(25);
sprintf(rocket_trace, "%s%s", passed_way, rocket);
passed_way[i] = ' ';
printf("\033[A");
printf("| %s\n", rocket_trace);
}
// erase a line and write a new line
printf("\033[A");
printf("\033[2K");
puts("Good luck!");
return 0;
}
(私の好み)の後に、ファイルをコンパイルし実行して、削除します
$ gcc timeout.c -o timeout && ./timeout && rm timeout
あなた自身で実行して、結果を確認してください。
注:
テスト環境
$ uname -a
Linux wlysenko-Aspire 3.13.0-37-generiC#64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
関連する問題
- 1. C#でタイマーを使うには?
- 2. Javaネットゲームでタイマーを使うには?
- 3. C#でタイマーを作成するには?
- 4. C#で意図したタイマーを使用するようにします
- 5. ルアでタイマーを使う方法
- 6. ブーストスレッドとタイマー、C++
- 7. UnixのC++タイマー
- 8. タイマーfor cラベル#
- 9. C++タイマー問題
- 10. ASP.NETでの定期的なイベントにC#タイマーを使用する
- 11. Kinect機能を使用したC#でのタイマーの使用
- 12. Unity:複数のタイマーの使用C#
- 13. どのようにCのタイマーの配列をゲートにする
- 14. VS-C#タイマー精度
- 15. C#の10分タイマー
- 16. C++のタイマーの例
- 17. タイマーとWindows Serice c#
- 18. C:複数のタイマー
- 19. STM32F4 - 非隣接タイマーを制御するためにタイマーを使う
- 20. C#他のタイマーの中のタイマーを使用していますか?
- 21. C#でタイマーを使用してアクションをトリガーする
- 22. タイマーを使ってシリアルポートCにデータを送る#
- 23. タイマーでC#メソッドを実行するには?
- 24. macOSでCタイマーを作成する
- 25. C# - どのようにタイマーを停止しますか?
- 26. Cantは正常に動作するようにタイマーを取得するC++
- 27. UpdatePanelを使用してMasterPageでタイマーを使用するには?
- 28. コンソールアプリケーションでタイマーを使用するにはどうすればよいですか?
- 29. C#を使用してタイマーで画面をキャプチャする方法は?
- 30. C++タイマーからカスタムJavaタイマーを作成する
、投票を選択し、ファイルディスクリプタ...あなたは何をしたいですか? –
これは重複した質問です。 http://stackoverflow.com/questions/459691/best-timing-method-in-c – pcbabu
@Eddy_Emこれらの選択、投票、epollは何ですか?私はタイマーを5秒と仮定して設定したいと思います。この時間内にユーザーが入力すると、入力は有効と見なされます。 –