【问题标题】:Perl alarm() equivalent in C?C中的Perl警报()等效?
【发布时间】:2013-06-07 19:38:32
【问题描述】:

在 C for Linux 中 Perl 的 alarm() 等价物是什么? AFAIK 在 Windows 中没有原生的 alarm 函数,但是 Perl 提供了一个我并不好奇的解决方法。

对于不知道alarm的人:Perl alarm

编辑:我实际上需要毫秒精度的警报。以及我可以在线程中使用的那个(在多线程应用程序中)。

【问题讨论】:

标签: c linux perl alarm


【解决方案1】:

类似:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2014-11-06
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多