C中与Perl的alarm()函数等效的是什么?

3
什么是Linux C语言中等同于Perl的alarm()函数?据我所知,Windows没有本地的alarm函数,但是Perl有一个解决方法,不过我并不真正好奇。
对于那些不了解alarm函数的人:Perl alarm 编辑:实际上我需要毫秒级精度的定时器,并且要在线程中使用(在多线程应用程序中)。

2
这不就是alarm(3)吗? - Carl Norum
1
实时定时器系统调用:timer_create()、setitimer()、timer_delete() 看起来是你想要的。timer_create() 的 man 手册页有一个示例。 - jim mcnamara
http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html,http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_gettime.html,http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_delete.html - ysth
1个回答

2

类似这样:

unsigned int alarm (unsigned int secs, unsigned int usecs) {
   struct itimerval old, new;
   new.it_interval.tv_usec = 0;
   new.it_interval.tv_sec = 0;

   // usecs should always be < 1000000
   secs += usecs / 1000000;
   usecs = usecs % 1000000;

   // set the alarm timer
   new.it_value.tv_usec = (long int) usecs;
   new.it_value.tv_sec = (long int) secs;

   // type ITIMER_REAL for wallclock timer
   if (setitimer (ITIMER_REAL, &new, &old) < 0)
     return 0;
   else
     return old.it_value.tv_sec;
 }

请参见:http://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接