Timer that Launches a Method at Regular Intervals

K

KevinSimonson

In Java one can set up a timer, and using that timer arrange for
methods to be called at regular intervals, say every five seconds. I
understand I can do that in C++ with the <sleep()> function, but that
would tie up the CPU, wouldn't it? And keep other functions from
running while the <sleep()> function is executing? I want to be able
to schedule my method to run at regular intervals, but I also want it
so that other functions can run while an interval is waiting to
complete. Is that possible with C++? And if so, how does one do it?

Kevin S
 
V

Victor Bazarov

In Java one can set up a timer, and using that timer arrange for
methods to be called at regular intervals, say every five seconds. I
understand I can do that in C++ with the<sleep()> function, but that
would tie up the CPU, wouldn't it?

It actually depends on the implementation of the [non-standard] sleep()
functions, doesn't it?
> And keep other functions from
running while the<sleep()> function is executing?

For that you need threading, as I understand it.
> I want to be able
to schedule my method to run at regular intervals, but I also want it
so that other functions can run while an interval is waiting to
complete. Is that possible with C++? And if so, how does one do it?

It is possible, but you need to use some kind of platform-specific
mechanism (like in Java you use Java Platform specific functionality),
which unfortunately is off-topic here. Ask in the newsgroup that caters
to your OS.

V
 
L

Luc Danton

In Java one can set up a timer, and using that timer arrange for
methods to be called at regular intervals, say every five seconds. I
understand I can do that in C++ with the<sleep()> function, but that
would tie up the CPU, wouldn't it? And keep other functions from
running while the<sleep()> function is executing? I want to be able
to schedule my method to run at regular intervals, but I also want it
so that other functions can run while an interval is waiting to
complete. Is that possible with C++? And if so, how does one do it?

Kevin S

With C++0x I imagine something like this could do what you want:

#include <thread>

template<typename Func, typename Duration>
void defer(Func&& func, Duration dur)
{
std::thread([dur](Func&& func)
{
for(;;) {
func();
std::chrono::this_thread::sleep_for(dur);
}
}, std::forward<Func>(func)).detach();
}

and use e.g. std::bind to call methods on objects.

Boost proposes a portable thread library that works with C++03 and and
some date/time utilities to go with that should do the same.
 
L

Luc Danton

In Java one can set up a timer, and using that timer arrange for
methods to be called at regular intervals, say every five seconds. I
understand I can do that in C++ with the<sleep()> function, but that
would tie up the CPU, wouldn't it? And keep other functions from
running while the<sleep()> function is executing? I want to be able
to schedule my method to run at regular intervals, but I also want it
so that other functions can run while an interval is waiting to
complete. Is that possible with C++? And if so, how does one do it?

Kevin S

With C++0x I imagine something like this could do what you want:

#include <thread>

template<typename Func, typename Duration>
void defer(Func&& func, Duration dur)
{
std::thread([dur](Func&& func)
{
for(;;) {
func();
std::chrono::this_thread::sleep_for(dur);

This is supposed to be:
std::this_thread::sleep_for(dur);
 
J

Juha Nieminen

Luc Danton said:
for(;;) {
func();
std::chrono::this_thread::sleep_for(dur);
}

Note that that will not achieve the desired effect if func() takes
a significant amount of time to execute.

For example, if you want func() called each 5 seconds, and func()
itself takes 2 seconds to execute, it will get called each 7 seconds
instead.

Of course if you want func() be called each 5 seconds regardless of
how long func() itself takes to execute, that's a more complicated
problem. (And of course there's always the question of what should be
done if func() takes more than 5 seconds to execute. One solution to
that is that if that's so, then the calling mechanism simply waits for
it to terminate without calling it again, so it's up to it to take less
than the desired call interval. However, the problem itself is still
complicated.)
 
H

Howard Hinnant

  Note that that will not achieve the desired effect if func() takes
a significant amount of time to execute.

  For example, if you want func() called each 5 seconds, and func()
itself takes 2 seconds to execute, it will get called each 7 seconds
instead.

  Of course if you want func() be called each 5 seconds regardless of
how long func() itself takes to execute, that's a more complicated
problem. (And of course there's always the question of what should be
done if func() takes more than 5 seconds to execute. One solution to
that is that if that's so, then the calling mechanism simply waits for
it to terminate without calling it again, so it's up to it to take less
than the desired call interval. However, the problem itself is still
complicated.)

In the case that the time of func() is consistently less than the time
of dur, std::this_thread::sleep_until, which takes a time_point
instead of duration, will handily solve the problem.

If func() sometimes takes longer than dur, then I agree you've got a
design decision to make.

-Howard
 
A

AnonMail2005

In Java one can set up a timer, and using that timer arrange for
methods to be called at regular intervals, say every five seconds.  I
understand I can do that in C++ with the <sleep()> function, but that
would tie up the CPU, wouldn't it?  And keep other functions from
running while the <sleep()> function is executing?  I want to be able
to schedule my method to run at regular intervals, but I also want it
so that other functions can run while an interval is waiting to
complete.  Is that possible with C++?  And if so, how does one do it?

Kevin S

Check out the boost::asio. It has synchronous and asynchronous
timers.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top