每次删除定时器都需要调用timer_delete吗? [英] Does timer_delete need to be called to delete the timer every time?

查看:34
本文介绍了每次删除定时器都需要调用timer_delete吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下代码中使用了 timer_create().它只会在 10 秒后触发一次处理程序.

I used timer_create() in the following code. It will trigger the handler only once after 10 seconds.

struct itimerspec itimer = { { 0, 0 }, { 10, 0 } };
struct sigevent si;

memset (&si, 0, sizeof (struct sigevent));
si.sigev_notify = SIGEV_THREAD;
si.sigev_notify_attributes = NULL;
si.sigev_notify_function = t;

if (timer_create (CLOCK_REALTIME, &si, &timer) < 0)
{
    fprintf (stderr, "[%d]: %s\n", __LINE__, strerror (errno));
    exit (errno);
}
if (timer_settime (timer, 0, &itimer, NULL) < 0)
{
    fprintf (stderr, "[%d]: %s\n", __LINE__, strerror (errno));
    exit (errno);
}

我的问题是,10 秒后我的处理程序被触发 - 现在我是否必须在退出进程之前使用 timer_delete() 删除计时器?或者,既然只触发了一次,就不用显式删除了吗?

My question is, after 10 seconds my handler got triggered - now do I have to delete the timer using timer_delete() before exiting the process? Or, since it is triggered only once, is there no need to delete it explicitly?

推荐答案

是的,您需要明确删除计时器.

查看 man(2) 页面的 timer_create.在注释部分(特别是第三个注释),您将看到每次调用 timer_create 都使用内核中的资源,以及所有中的定时器总数内核可以一次性分配的进程是有限的.

Take a look at the man(2) page for timer_create. In the notes section (the third note, specifically), you'll see that every call to timer_create uses resources in the kernel, and the total number of timers, across all processes, that can be allocated by the kernel at once is limited.

如果你不删除你的定时器,你最终会用完它们并且任何需要分配定时器的应用程序都可能会失败.

If you don't delete your timers, you will eventually run out of them and any applications that need to allocate timers may fail.

这就像内存泄漏 - 清理您使用的资源,否则您最终会耗尽.

It's just like memory leaks - clean up the resources you use, or you'll eventually run out.

回复您的后续问题
在下面的评论中,您询问是否可以从回调函数内部调用 timer_delete.我不确定如何回应,所以我自己打开了关于它的问题.答案似乎是也许.您可以尝试对其进行试验,看看它是否有效,但我建议您不要这样做.我从未见过任何从回调中删除计时器的代码,在事件处理完成之前释放计时器资源的想法让我感到紧张.

Reply to your follow-up question
In the comments below, you asked if it is okay to call timer_delete from inside your callback function. I was unsure of how to respond, so I opened question about it myself. The answer seems to be maybe. You can try to experiment with it and see if it works, but I would recommend against it. I've never seen any code that deletes the timer from within the callback, and the idea of deallocating the timer resources before an event has finished being handled makes me nervous.

此外,有时对其进行测试可能会产生不错的结果,但由于您正在处理异步事件,因此会出现随机故障.此外,您的主程序需要一直运行直到回调完成(它们运行在同一个进程中,只是不同的线程),因此您最好在退出之前删除主线程中的计时器.我认为这是更安全的解决方案,并且更容易调试.

Also, testing it may yield okay results sometimes, but have random failures since you're dealing with asynchronous events. Furthermore, your main program needs to be running until the callback completes anyway (they run in the same process, just different threads), so you may as well delete the timer in your main thread right before exiting. I think it's the safer solution, and will be easier to debug.

这篇关于每次删除定时器都需要调用timer_delete吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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