I am working on a timer now, timers are maintained by a linked list.Like this:
struct timer
{
struct timer* prev;
struct timer* next;
struct timespec start;
struct timespec interval;
void* par;
int (*handler) (void* par);
};
Then I use a thread called dispatch
to sleep and pick timers from the list.The code is simplified as below:
//just pseudo code here
void dispatch() {
for (;;) {
while (list_empty()) {
wait();
}
timer* ptr = listhead();
if (timer_begin(ptr)) {
ptr->handler(ptr->par);
list_pop_front();
}
}
}
Question:
When there is a long precedure in the callback function handler
(say the handler function costs 500ms to execute, then dispatch
stucks), the rest timers in the list may not be processed in time.So do we need a thread pool
here?Or is there any other advices?