Use of the thread pool

Source: Internet
Author: User

The Tbox thread pool optimizes the competition for locks by pulling multiple tasks in batches at a time in each worker.

Because the function implementation of each task is not too much, you can hash the function address of each task to calculate the average time spent on each task execution. Then, based on the average, the number of tasks that each worker pulls at a time is calculated dynamically, and the Tbox has a default of 10s of tasks per worker at a time, which avoids frequent preemption of the worker locks as much as possible.

All of the tasks pulled from the waiting queue are placed in the pending queue, and if a worker is idle when a task in the waiting queue is exhausted, it attempts to go to the pending and re-pulls the task that the other worker has not yet performed. This solves the problem that some tasks take too long to block the remaining tasks in the worker.

The ability to re-pull other worker tasks from the pending queue is not maintained by locks, but by atomic operations to determine the state of the task, so performance is guaranteed.

The entire thread pool, which uses only one lock to maintain several internal queues, each worker runs independently in most cases, and only goes back to the global wait queue for tasks when all of its tasks are idle, and the upper interface also provides the interface for the bulk delivery task. To minimize the use of locks.

Let's take a look at the simple use example:

Static tb_void_t Tb_demo_task_time_done (tb_cpointer_t priv) {tb_msleep ((tb_size_t) (PRIV));} Static tb_void_t Tb_demo_task_time_exit (tb_cpointer_t priv) {}/* post a 60s task to the global thread pool * * Tb_thread_pool (): Global thread Pool instance, If you do not want to use the global, you can also create a thread pool * "60000ms": Specify a task name, constant String * Tb_demo_task_time_done: Task function Address * Tb_demo_task_time_exit: Cleanup function when the task is executed or canceled, can be used to free some of its own data, this is optional, not directly tb_null * (tb_cpointer_t) 60000: Pass the private data pointer, here simply passed a wait time value in * Tb_false: Whether it is an urgent task, if it is tb_true, the task will take precedence as soon as possible */tb_thread_pool_task_post (Tb_thread_pool (), "60000ms", tb_demo_task_time_ Done, Tb_demo_task_time_exit, (tb_cpointer_t) 60000, tb_false);//deliver a 10s emergency mission Tb_thread_pool_task_post (Tb_thread_ Pool (), "10000ms", Tb_demo_task_time_done, Tb_null, (tb_cpointer_t) 10000, tb_true);//Bulk delivery of two tasks tb_thread_pool_task_t List[2] = {0};list[0].name = "60000ms"; list[0].done = Tb_demo_task_time_done;list[0].exit = Tb_demo_task_time_exit; List[0].priv = (tb_pointer_t) 60000;list[0].urgent = Tb_false;list[1].name = "10000ms"; list[1].done = Tb_demo_task_timE_done;list[1].exit = Tb_null;list[1].priv = (tb_pointer_t) 10000;list[1].urgent = Tb_true;tb_thread_pool_task_post_ List (Tb_thread_pool (), list, 2);//Initialize and post a 10s emergency task, return a valid handle tb_thread_pool_task_ref_t task = Tb_thread_pool_task_ Init (Tb_thread_pool (), "10000ms", Tb_demo_task_time_done, Tb_null, (tb_cpointer_t) 10000, tb_true); if (Task) {//Cancel this task    , if the task is already in operation, it is impossible to cancel the Tb_thread_pool_task_kill (Tb_thread_pool (), Task);    Wait for the task to cancel or complete, timeout:-1: Infinite Wait tb_thread_pool_task_wait (Tb_thread_pool (), task,-1); Release this task Tb_thread_pool_task_exit (Tb_thread_pool (), Task);}

If you do not want to use the global thread pool, you can initialize one yourself:

/* Initialize thread pool * * 8: Maximum number of worker, upper value, if 0 is using default value * 0: Stack size per worker thread, if pass 0 is using default value */tb_thread_pool_ref_t Thread_pool = Tb_thre Ad_pool_init (8, 0); if (thread_pool) {    //Post a 10s emergency task    tb_thread_pool_task_post (Thread_pool, "10000ms", tb_ Demo_task_time_done, Tb_null, (tb_cpointer_t) 10000, tb_true);    If in debug mode, you can dump the entire thread pool state and all processing tasks in the state #ifdef __tb_debug__    tb_thread_pool_dump (thread_pool); #endif    // Wait for all tasks to complete or be canceled    Tb_thread_pool_task_wait_all (Thread_pool,-1);    Exit thread pool    tb_thread_pool_exit (Thread_pool);}
    • Tbox Project Details
    • Tbox Project Source Code
    • Tbox Project Documentation

Use of the thread pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.