This is the fifth day of my participation in the August Wen Challenge.More challenges in August
timer
Timers are used in various scenarios to execute scheduled tasks. Let’s take a look at the tbox timer.
tbox
Usage Scenarios:
- Provide high precision timer
tb_timer
tb_timer_ref_t tb_timer(tb_noarg_t);
Copy the code
Global timer
tb_timer_init
tb_timer_ref_t tb_timer_init(tb_size_t grow, tb_bool_t ctime);
Copy the code
Initializing timer
tb_timer_exit
tb_void_t tb_timer_exit(tb_timer_ref_t timer);
Copy the code
Exit timer
tb_timer_kill
tb_void_t tb_timer_kill(tb_timer_ref_t timer);
Copy the code
Kill timer
tb_timer_clear
tb_void_t tb_timer_clear(tb_timer_ref_t timer);
Copy the code
Cleaning timer
tb_timer_delay
tb_size_t tb_timer_delay(tb_timer_ref_t timer);
Copy the code
Extension timer
tb_timer_top
tb_hize_t tb_timer_top(tb_timer_ref_t timer);
Copy the code
Returns when the timer is executed
tb_timer_spak
tb_bool_t tb_timer_spak(tb_timer_ref_t timer);
Copy the code
Spak timer for a single-threaded external loop
tb_timer_loop
tb_void_t tb_timer_loop(tb_timer_ref_t timer);
Copy the code
Loop timer for external threads
tb_timer_task_post
tb_void_t tb_timer_task_post(tb_timer_ref_t timer, tb_size_t delay, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv);
Copy the code
A posttimer task that is delayed will be automatically deleted after it expires
tb_timer_task_post_at
tb_void_t tb_timer_task_post_at(tb_timer_ref_t timer, tb_hize_t when, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv);
Copy the code
The timer task is deleted by absolute time and expires after it is automatically deleted
tb_timer_task_post_after
tb_void_t tb_timer_task_post_after(tb_timer_ref_t timer, tb_hize_t after, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv);
Copy the code
The timer task is deleted according to the relative time and expires after it is automatically deleted
tb_timer_task_init
tb_timer_task_ref_t tb_timer_task_init(tb_timer_ref_t timer, tb_size_t delay, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv);
Copy the code
To initialize the delay timer task, manually delete it
tb_timer_task_init_at
tb_timer_task_ref_t tb_timer_task_init_at(tb_timer_ref_t timer, tb_hize_t when, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv);
Copy the code
The timer task is initialized by absolute time and needs to be manually deleted
tb_timer_task_init_after
tb_timer_task_ref_t tb_timer_task_init_after(tb_timer_ref_t timer, tb_hize_t after, tb_size_t period, tb_bool_t repeat, tb_timer_task_func_t func, tb_cpointer_t priv);
Copy the code
The timer task is initialized by relative time and needs to be manually deleted
tb_timer_task_exit
tb_void_t tb_timer_task_exit(tb_timer_ref_t timer, tb_timer_task_ref_t task);
Copy the code
Exits the timer task. If the task has not been invoked, the task will not be invoked
tb_timer_task_kill
tb_void_t tb_timer_task_kill(tb_timer_ref_t timer, tb_timer_task_ref_t task);
Copy the code
Kills the timer task and does not invoke it if it has not already been invoked
You can do this by setting
The global default tb_timer() posts timer tasks, and tbox creates a single thread in the background to maintain all its tasks.
Create a separate timer and connect it to your loop thread to reuse thread resources: