I want to implement some system in c++ so that I can call a function and ask for another function to be called in X milliseconds. Something like this:
callfunctiontimed(25, funcName);
25 being the amount of milliseconds before the function should be called.
I would like to know if multithreading is required for this and then use some delay function? Other than using function pointer how would a feature like this work?
Many folks have contributed good answers here on the matter, but I will address the question directly, because I had a similar problem a couple of years ago. I could not use Boost for several reasons--I know Boost has excellent use in a lot of open source software. Moreover, I really wanted to understand timers and callbacks specifically as it pertains to Linux based environments. So, I wrote my own.
Fundamentally, I have a
Timer
class and aTimerCallback
class. A typical callback, implemented as a inherited class of theTimerCallback
class, will place the operations to be executed upon callback in thetriggered ()
method, implemented specifically for the needs.Per the usual semantics, a
Timer
object is associated with a callback object, which presumably contains all the required information needed for the callback to execute. The timer scheduling is managed by one environment-wide timer minheap which has to be maintained in a separate thread/process. This minheap task does only one thing: it minheapifies the minheap of callback events set in the future. A minheap selects the next event to fire inO(1)
and can minheapify the remaining inO(log n)
forn
timer events. It can also insert a new timer event inO(log n)
(Read a gentle introduction to heaps here).When a timer fires, the minheap scheduler checks if it is a periodic timer, one shot timer or a timer that will execute a specific number of times. Accordingly, the timer object is either removed from the minheap or reinserted back into the minheap with the next execution time. If a timer object is to be removed, then it is removed from the minheap (but the timer object deletion may or may not be left to the task that created it) and the rest of the heap is minheap-ified; i.e., rearranged to satisfy the minheap property.
A working and unit tested implementation is here, and may contain bugs (excerpted from my application), but I thought it may help someone. The implementation is multi-process (
fork()
ed-process) based (and also usespthread
s in the main task (process)), and uses POSIX shared memory and POSIX message queues for communication between the processes.For a portable solution, you can use boost::asio. Below is a demo I wrote a while ago. You can change
to suit you need say make function call after 200 milliseonds.
Below is a complete working example. It's calling repeatedly but I think it should be easy to call only once by just change a bit.
Do you want it to by asynchronous so that the callback gets executed when 25 miliseconds are over without blocking the main executing thread? If so, you can execute the callback in a separate thread from the timer / timed callback function you implement.
If you do not use multithreading, then your main or the calling function of callfunctiontimed(25, funcName); would block while you run a sleep / usleep. It is your choice now as to what behavior you want to implement.
The real solution would not be as simple as to multithread or not. There are things like, how do you keep different timers/callbacks information considering the function can be called multiple times with different timeouts and functions.
One way to do it, would be like this:
As the timer thread is done sleeping, it removes and looks at head of the list and executes the function pointer in a new thread. Timer thread is re-initialized with sleep time on the new head of the list.
Hopefully this gives an idea of what I meant, although this is not perfect and has several problems.
In windows, you have
SetTimer
- http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspxSample code: