开发者

how to implement timer in C/C++

I want to call a function of the business class after every 2 hours. I m not getting any way to implement sam开发者_如何学JAVAe in C/C++ without using a while loop. My problem is that i cannot use while(1) as this does not retun back the control for further execution.

Any pointer in this regards wud be helpful....:)

thnaks


Boost.Asio provides Timers.


In plain C, I would've considered using the alarm(2) or setitimer(2) functions. Alternatively, spawn a thread and do the waiting from there.

If you decide on the alarm or setitimer routes, bear in mind that you'll need to write signal handlers and may need a dispatch loop to note that it is time to do the periodic maintenance calls, as it's considered bad practise to do quite a few things from within a signal handler.


For a general solution, you could start a thread, which sends some message to the main thread after 2 hours.

For linux, you could use this:

http://linux.die.net/man/3/alarm

and then handle the SIGALRM signal

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜