Is There a C++ Timer Like the java.util.Timer

V

Victor Bazarov

In java, they have a class where you can create a timer which will
repeatedly call a routine after a time period. Is there something
like this in C++ Visual Studio 2010?

In C++ there is no such thing, but there can be something in the library
for your platform (MS Windows, I am guessing). See 'SetTimer' Windows
API function.

V
 
K

K. Frank

Hello clusardi2 (and Victor)!

In C++ there is no such thing, but there can be something in the library
for your platform (MS Windows, I am guessing). See 'SetTimer' Windows
API function.

Victor, or course, has answered your question -- no, not in
standard C++, but yes in the win32 api.

Having said that, it is worth noting that C++11 does introduce
the very basic functionality of being able to sleep for a
certain amount of time:

void std::this_thread::sleep_until(...)
void std::this_thread::sleep_for(...)

Prior to C++11, the standard was silent on sleep-type functions.


Happy Hacking!


K. Frank
 
V

Victor Bazarov

Hello clusardi2 (and Victor)!



Victor, or course, has answered your question -- no, not in
standard C++, but yes in the win32 api.

Having said that, it is worth noting that C++11 does introduce
the very basic functionality of being able to sleep for a
certain amount of time:

void std::this_thread::sleep_until(...)
void std::this_thread::sleep_for(...)

Prior to C++11, the standard was silent on sleep-type functions.

To add, there is a whole bunch of timer/clock/chrono functions in C++11
(see subclause 20.11 of the Standard), but none of them are designed to
make use of a callback, so you'd have to "roll your own" if your OS API
doesn't provide you with anything suitable.

V
 
K

K. Frank

Hi Paavo!

For making a timer-based callback there must be some kind of event loop
which could receive the callback.

I would disagree with this (although this could be a purely
semantic disagreement about what "callback" means).

To me, "callback" is short for "callback function" which is
a function that is given to some other piece of code (by
passing a function pointer or some kind of function object)
that gets called by the other code when something happens
(such as when a timer fires).

But this function can be called purely synchronously by the
other code (which may or may not be running in its own thread).

So, for example, if you have a timer facility that runs in
its own timer thread, your code running it its own thread
(perhaps the "main" thread of the program) could could
schedule a callback to be called by the timer facility in,
say, five seconds. After five seconds your callback gets
called, but it gets called on the timer thread. Your code
does not have to provide any kind of event loop.
In GUI programs such an event loop is
(almost?) always present, so it can be re-utilized easily. C++ however is
defined on a more general level and cannot assume presence of a GUI or an
event loop.

In C++11, a standard way to do this would indeed be to start a new timer
thread. The thread would sleep for or until the specified time between the
tasks. If there is an event loop present, the timer thread could send a
specific event to it.

Yes, but ...
If there is no event loop, the timer thread could
perform the periodic task by itself.

Precisely. In this case I would say that the timer thread
executed the callback function.
There are many possibilities how to
set such a thing up.

(I will note that in many cases where you have actions
scheduled to be performed by a timer it is likely that
your overall use case will be "event driven," so it would
be natural to have a formal event loop. But in such
cases I would be more likely -- semantics again -- to
speak of events being scheduled, rather than callbacks
being registered or scheduled.)
...
Cheers

Paavo


Cheerio!


K. Frank
 
V

Victor Bazarov

There certainly is. See <chrono> and <thread>. Spawn a thread, then use
the facilities in <chrono> to poke something, at regular intervals.

In other words, roll your own. There is no mechanism for which you only
need a callback function (or functor) and a set-up call. Thanks for
confirming.

V
 
V

Victor Bazarov

Do you think that java.util.Timer is just a single function call, too?

Well, I couldn't really care less, even if I tried. I was comparing
your proposal with MS Windows API's SetTimer, the only familiar to me
solution.

V
 
J

James Kanze

For making a timer-based callback there must be some kind of event loop
which could receive the callback.

Why?

I implemented this functionality in an OS way back around 1980
(not in C++, of course), and there was no event loop. For that
matter, there is no event loop in Java, and they implement it.
In GUI programs such an event loop is (almost?) always
present, so it can be re-utilized easily. C++ however is
defined on a more general level and cannot assume presence of
a GUI or an event loop.

Nor can Java. What Java *can* count on is threads. According
to the documentation, each Timer object is a thread.
In C++11, a standard way to do this would indeed be to start a new timer
thread. The thread would sleep for or until the specified time between the
tasks. If there is an event loop present, the timer thread could send a
specific event to it. If there is no event loop, the timer thread could
perform the periodic task by itself. There are many possibilities how to
set such a thing up.

You seem to be fixed on event loops. According to the specs for
java.util.Timer, the action is executed in the timer thread.
(FWIW: I don't think it would take more than about 10-15 lines
of code to implement the equivalent in C++11.)
 
J

Jorgen Grahn

.
This is a fundamental difference between C++ and Java. Java tends to throw
everything into its standard's kitchen sink. C++ standard's scope is much
smaller, and coverts a tiny subset of comparable functionality of Java's
standard. C++ tends to fob off all the functionality to add-on libraries.

Put differently, Java is its own platform, on top of whatever platform
you're using. C++ doesn't have much of that, even if C++11 has more.
The argument which approach is better will rage forever; but the argument
that there is no comparable facility in C++ is bogus. There is, you just
have to have either the right library, which includes either the base
implementation's library whose scope exceeds the one in the standard, or
some add-on library. There's the single call you're looking for.

Sometimes the question of whether something this or that is in the standard
is the wrong question to ask. The question should be, do I have it in my
implementation. Only a small number of potential use cases would require
implementation independence. Unless this is an explicit requirement, whether
something's in the standard, or not, is immaterial.

Yes. My work is almost exclusive in Unix (Posix, Linux ...). I have
the literature, the experience, the terminology and the mindset. C++
(and C of course, and to a large extent Perl, Python and similar
languages) lets me reuse all of that, and I'm grateful for it.

It is sometimes embarrassing to have to admit that Standard C++
doesn't have feature FOO, but this is the other side of the coin.

/Jorgen
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top