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.)
Cheerio!
K. Frank