0

I want to call a function of the business class after every 2 hours. I m not getting any way to implement same 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

3
  • 1
    The answer will depend on what platform you're on. Linux? Windows? Something else? Commented Nov 1, 2010 at 7:31
  • Just curious. What does your program do for 2 hours if it's not already implemented as an event loop? Commented Nov 1, 2010 at 7:33
  • I m trying to achieve this on Mac Os X and actually it is a background process so it runs always in backgroung as long as your system is up. Commented Nov 2, 2010 at 4:45

3 Answers 3

2

Boost.Asio provides Timers.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.