Could your please help me with following interview question.
Given function Sleep(int seconds) implement following interface so timers could be used:
- function
void CreateTimer(void (*func)(), int seconds)that her purpose is to create timer - function
void StartTimers()that her purpose to start all the timers
Every timer that started should delay for several seconds and then use a callback to call a function.
Example:
CreateTimer(func1,3);
CreateTimer(func2,7);
CreateTimer(func3,10);
StartTimers()
The folowing should be happening:
Delay for 3 seconds and then call for function 1.
Delay for 4 seconds and then call for function 2.
Delay for 3 seconds and then call for function 3.
The question is how implement such interface?
EDIT 1: Use the questions API.
EDIT 2: Oops, didn’t call q.pop();
This sounds like a job for
std::priority_queue, ordered by deadline.