Use of high-precision timer

2015/12/21 12:54
Reading number 1.1K

There are two timer implementations in tbox: timer and ltimer

  • 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)

This article mainly explains how to use timer to implement high-precision timer tasks, accurate to the ms level. For low precision ltimer, please refer to: Use of low precision timer

Here is a simple example:

 /*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 above delivery will start the timing and running of the task immediately after the post is called. If you want to start the timer after 10s, you can deliver as follows:

 //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);

If you want to determine the absolute delivery time, you can do this:

 //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);

The previously used post delivery interface cannot maintain and control tasks. If you want to cancel the timer task in the execution queue at a specific time

The following methods can be used to maintain:

 //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);

The previous examples are all in the global default tb_timer() For timer tasks posted in, tbox will create a separate thread in the background to maintain all its tasks. If it feels that this takes too much resources and has its own specific thread constantly looping, it can create an independent timer, hook it to its own loop thread, and reuse thread resources:

 //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); }

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
two Collection
zero fabulous
 Back to top
Top