Li bangzhu
[Email protected]
1. Timer Introduction
A timer usually contains at least two members: a time-out period (usually relative time or time-out period) and a callback function after the time-out period expires. Sometimes it may include the parameters that need to be passed in when the callback function is executed, whether to restart the timer, and how to change the timer timeout. If you use a linked list as a container to concatenate all the timers, each timer also contains pointer members pointing to the next timer. Further, if the linked list is bidirectional, each timer also needs to contain a pointer member that refers to the previous timer.
2. Implementation of the ascending timer linked list
# Ifndef lst_timer # define lst_timer # include <time. h> # define buffer_size 64 class timer;/* Forward Declaration */* // * user data structure, including IP address, port, file descriptor, read cache, and the timer */struct client_data {char IP [buffer_size]; int port; int sockfd; char Buf [buffer_size]; timer * timer ;}; /* Timer class */class timer {public: timer (): Prev (null), next (null) {} public: time_t expire;/* timeout time of the task, use absolute time */client_data * user_data; void (* cb_func) (client_data *);/* task callback function */Timer * Prev;/* Indicates a forward timer */Timer * Next;/* indicates the next timer */void * ARG;/* can be expanded as needed */}; /* timer linked list, which is an ascending, bidirectional linked list with a header node and a tail node */class sort_timer_lst {public: sort_timer_lst (): Head (null), tail (null) {}~ When sort_timer_lst ()/* is destroyed, delete all timer */{timer * TMP = head; while (TMP) {head = TMP-> next; Delete TMP; TMP = head ;}} void add_timer (timer * timer);/* Add the target timer to the linked list */bool find_timer (timer * timer ); /* Find the target timer */void del_timer (timer * timer);/* Delete the target timer */void adjust_timer (timer * timer);/* When a scheduled task changes, adjust the corresponding timer position in the linked list. This function is used to backward adjust */void tick (); Private: void add_timer (timer * timer, Timer * head); Private: timer * head; timer * tail;}; # endifLst_timer.cpp
# Include "lst_timer.h" Void sort_timer_lst: add_timer (timer * timer) {If (! Timer) return; If (! Head) {head = tail = timer; return;} If (timer-> expire
3. Performance Analysis
Sort_timer_lst is an ascending linked list. Its core function is tick, which is equivalent to a pulse. It is executed every other time to detect and process expired tasks. The reason for determining that a scheduled task expires is that the timed expire value is less than or equal to the current time. In terms of execution efficiency, the time complexity of the task to add a timer is O (n), the time complexity of deleting the timer is O (1), and the time complexity of executing the task is O (1 ).
4. other high-performance timers
The timer based on the sort linked list has an efficiency problem, and the efficiency of adding a timer is low. Therefore, you can use the time wheel or time heap to solve this problem. Interested friends can learn the time wheel and time heap.
5. For more information, go to the personal library
Http://wenku.baidu.com/p/helpylee