异步,定时功能调用C对Linux呢? [英] Asynchronous, timed function calling in C on Linux?

查看:120
本文介绍了异步,定时功能调用C对Linux呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Linux下C的最简单,最有效的方法,可以异步定期调用有一定的延迟后的函数(如JavaScript的的setTimeout )或设置重复计时器调用它(类似的setInterval )?

What is the easiest, most effective way in C on Linux to asynchronously call a function after a certain delay (like JavaScript's setTimeout) or set a repetitive timer call it periodically (similar to setInterval)?

尽管这个问题适用于Linux,我希望有就是跨平台的方法。

Though this question applies to Linux, I hope there is a method that is cross-platform.

推荐答案

最简单的Linux特有的解决方案是使用报警功能:

The simplest Linux specific solution is to use the alarm function:

void alarm_handler (int signum)
{
    printf ("Five seconds passed!\n");
}

signal (SIGALRM, alarm_handler);
alarm (5);

getitimer setitimer函数函数可以用来创建集成到更precisions定时器。 (更多... )。

getitimer and setitimer functions can be used to create timers with higher-precisions. (more...).

这篇关于异步,定时功能调用C对Linux呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆