windows C 定时器回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/866622/
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
C Timer Callback
提问by singpolyma
Interested in something similar to JavaScript setTimeout in C on both UNIX and Windows.
对 UNIX 和 Windows 上的 C 语言中类似于 JavaScript setTimeout 的内容感兴趣。
Basically, I want:
基本上,我想要:
start_timer(&function_pointer, int time_in_secs)
or as close to that as I can get.
或尽可能接近那个。
Also, something similar to setInterval would be nice (where it calls the callback every n seconds), but that can be implemented using setTimeout :)
此外,类似于 setInterval 的东西会很好(它每 n 秒调用一次回调),但这可以使用 setTimeout :)
回答by scottm
回答by Robert S. Barnes
You might want to try the POSIX interval timers, timer_create and timer_settime, as it allows you to specify a call back function directly without using signals. To get the timer to expire just once ( and not keep repeating ):
您可能想尝试 POSIX 间隔计时器 timer_create 和 timer_settime,因为它允许您直接指定回调函数而无需使用信号。让计时器只过期一次(而不是不断重复):
timer_settime: The reload value of the timer shall be set to the value specified by the it_interval member of value. When a timer is armed with a non-zero it_interval, a periodic (or repetitive) timer is specified.
timer_settime:定时器的重载值应设置为 value 的 it_interval 成员指定的值。当计时器配备非零 it_interval 时,指定一个周期性(或重复)计时器。
Here is extensive documentation of using these timers with a nice example from the Linux Programmer's Manual on kernel.org:
以下是使用这些计时器的大量文档以及 kernel.org 上 Linux 程序员手册中的一个很好的示例:
回答by Employed Russian
For UNIX, man setitimer
.
对于UNIX, man setitimer
。
回答by Lance Richardson
Some information about alarm() and setitimer() can be found here. Note that the upcall function is a signal handler, so it is subject to all of the limitations and restrictions associated with signal handlers as described in great detail here.
可以在此处找到有关 alarm() 和 setitimer() 的一些信息。需要注意的是上行调用函数是一个信号处理程序,所以它是受所有的非常详细描述与信号处理相关的限制和约束的位置。
As noted in the first link, alarm() is POSIX-based, setitimer() is not (although it is more flexible).
如第一个链接中所述,alarm() 是基于 POSIX 的,而 setitimer() 不是(尽管它更灵活)。
回答by viraptor
Try glib. If you need portable code, there's high probability that you cannot write anything more portable than them ;)
试试油嘴滑舌。如果您需要可移植的代码,很可能您无法编写比它们更可移植的代码;)
http://library.gnome.org/devel/glib/2.20/glib-The-Main-Event-Loop.html#g-timeout-add
http://library.gnome.org/devel/glib/2.20/glib-The-Main-Event-Loop.html#g-timeout-add