Linux C 中的循环/计时器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5540245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loops/timers in C
提问by system
How does one create a timer in C?
如何在 C 中创建计时器?
I want a piece of code to continuously fetch data from a gps parsers output.
我想要一段代码来连续从 gps 解析器输出中获取数据。
Are there good libraries for this or should it be self written?
是否有很好的库,还是应该自己编写?
采纳答案by Marko Kevac
Simplest method available:
可用的最简单方法:
#include <pthread.h>
void *do_smth_periodically(void *data)
{
int interval = *(int *)data;
for (;;) {
do_smth();
usleep(interval);
}
}
int main()
{
pthread_t thread;
int interval = 5000;
pthread_create(&thread, NULL, do_smth_periodically, &interval)
...
}
回答by Necrolis
回答by T.E.D.
One doesn't "create a timer in C". There is nothing about timing or scheduling in the C standard, so how that is accomplished is left up to the Operating System.
一个不是“在 C 中创建一个计时器”。C 标准中没有关于计时或调度的内容,因此如何完成由操作系统决定。
This is probably a reasonable question for a C noob, as many languages dosupport things like this. Ada does, and I believe the next version of C++ will probably do so (Boost has support for it now). I'm pretty sure Java can do it too.
对于 C 菜鸟来说,这可能是一个合理的问题,因为许多语言确实支持这样的事情。Ada 可以,而且我相信 C++ 的下一个版本可能会这样做(Boost 现在已经支持它)。我很确定 Java 也可以做到。
On linux, probably the best way would be to use pthreads. In particular, you need to call pthread_create()and pass it the address of your routine, which presumably contains a loop with a sleep()(or usleep()) call at the bottom.
在 linux 上,最好的方法可能是使用pthreads。特别是,您需要调用pthread_create()并将您的例程的地址传递给它,它大概包含一个循环,在底部调用sleep()(或usleep())。
Note that if you want to do something that approximates real-time scheduling, just doing a dumb usleep()
isn't good enough because it won't account for the execution time of the loop itself. For those applications you will need to set up a periodic timerand wait on that.
请注意,如果您想做一些接近实时调度的事情,仅仅做一个傻瓜usleep()
是不够的,因为它不会考虑循环本身的执行时间。对于这些应用程序,您需要设置一个定期计时器并等待它。
回答by Chris
The question about a timer is quite unspecific, though there are two functions that come to my mind that will help you:
关于计时器的问题非常不具体,尽管我想到了两个功能可以帮助您:
- sleep() This function will cause execution to stop for a specified number of seconds. You can also use usleep and nanosleep if you want to specify the sleeptime more exactly
- gettimeofday() Using this function you are able to stop between to timesteps.
- sleep() 此函数将导致执行停止指定的秒数。如果您想更准确地指定睡眠时间,也可以使用 usleep 和 nanosleep
- gettimeofday() 使用此函数,您可以在时间步长之间停止。
See manpages for further explanation :)
请参阅联机帮助页以获取进一步说明:)
回答by Null Set
SDLprovides a cross platform timer in C.
SDL在 C 中提供了一个跨平台计时器。
回答by Thomas M. DuBuisson
On POSIX systems you can create (and catch) an alarm. Alarm is simple but set in seconds. If you need finer resolution than seconds then use setitimer.
在 POSIX 系统上,您可以创建(并捕获)警报。闹钟很简单,但以秒为单位设置。如果您需要比秒更精细的分辨率,请使用setitimer。
struct itimerval tv;
tv.it_interval.tv_sec = 0;
tv.it_interval.tv_usec = 100000; // when timer expires, reset to 100ms
tv.it_value.tv_sec = 0;
tv.it_value.tv_usec = 100000; // 100 ms == 100000 us
setitimer(ITIMER_REAL, &tv, NULL);
And catch the timer on a regular interval by setting sigaction.
并通过设置sigaction定期捕获计时器。
回答by flumpet
If the gps data is coming from some hardware device, like over a serial port, then one thing that you may consider is changing the architecture around so that the parser kicks off the code that you are trying to run when more data is available.
如果 gps 数据来自某个硬件设备,例如通过串行端口,那么您可能需要考虑的一件事是更改架构,以便解析器在有更多数据可用时启动您尝试运行的代码。
It could do this through a callback function or it could send an event - the actual implementation would depend on what you have available.
它可以通过回调函数来做到这一点,也可以发送一个事件——实际的实现将取决于你有什么可用的。