-
Timer: High precision version, implemented with the minimum heap, complexity: O (log (n)) -
Ltimer: low precision version, using the timing wheel algorithm in the linux kernel, with a complexity of O (1)
/*Define a timer task processing function * *@ param killed indicates whether the current task was forcibly killed by tb_timer_task_kill *@ param priv User defined data pointer passed in during task delivery */ static tb_void_t tb_demo_timer_task_func(tb_bool_t killed, tb_cpointer_t priv) { } /*Send a timer task to the global timer every 1000ms, and it will be executed repeatedly * *Where tb_true indicates whether the execution will be repeated. If it is set to tb_false, it will be executed only once *The tb_null parameter is the user-defined data pointer passed to the task function */ tb_timer_task_post(tb_timer(), 1000, tb_true, tb_demo_timer_task_func, tb_null);
//The timer task will be delivered to the global timer after 10s. The timer task will be repeated every 1000ms tb_timer_task_post_after(tb_timer(), 10000, 1000, tb_true, tb_demo_timer_task_func, tb_null);
//Only after the specified timestamp tb_time()+150000 can a timer task be delivered to the global timer. The timer task will be executed repeatedly every 1000ms //This is equivalent to 15 seconds before delivery tb_timer_task_post_at(tb_timer(), tb_time() + 150000, 1000, tb_true, tb_demo_timer_task_func, tb_null);
//Create a timer task with an interval of 10s, which will not be executed repeatedly tb_timer_task_ref_t task = tb_timer_task_init(tb_timer(), 10000, tb_false, tb_demo_timer_task_func, tb_null); //After a period of time // ... //If you don't want to execute this task, you can cancel it manually. This is thread safe if (task) tb_timer_task_kill(tb_timer(), task); //Finally, destroy the task resource when the program exits if (task) tb_timer_task_exit(tb_timer(), task);
//Suppose this is your own thread static tb_pointer_t tb_demo_timer_loop(tb_cpointer_t priv) { // the timer tb_timer_ref_t timer = (tb_ltimer_ref_t)priv; #if 1 //Own thread loop while (1) { //Wait for a specific timer delay //It is not necessary to use sleep. If your thread is processing select/poll, you can directly use the timeout parameter of these interfaces to wait tb_sleep(tb_timer_delay(timer)); //Some other logic codes // ... //Fixed call. Pulse the timer. It is fast and will not block for a long time unless there is a time-consuming task tb_timer_spak(timer); } #else //If there is no other logic code, you can directly use the loop provided by timer tb_timer_loop(timer); #endif // exit it tb_thread_return(tb_null); return tb_null; } /*Create a timer * *First parameter: specify the maximum concurrent task size of the timer. By default, 0 can be passed. You can also specify the size number yourself *The second parameter: whether to enable timestamp cache optimization, just pass tb_false, which is generally used for optimization in high concurrency processing on the server side */ tb_timer_ref_t timer = tb_timer_init(0, tb_false); if (timer) { //Post some tasks // ... //Exit timer tb_timer_exit(timer); }