Use the C ++ language to design a scalable thread pool (2)

Source: Internet
Author: User

3,Task Allocation Policy

There are various task objects in service processing, and these business objects have different usage of system resources. Regardless of the space complexity of these tasks, from the perspective of thread execution tasks, the main concern is the time complexity.

When receiving a new task, the thread buffer pool first needs to look for Idle threads, input a new task, execute the task, delete the task, and set the idle thread flag. Looking for Idle threads, passing in tasks, and final cleanup tasks are all additional overhead for task execution. If most of the executed tasks are lightweight tasks, the resource waste caused by additional overhead becomes very prominent. To solve this problem, you can input N5 lightweight tasks to a thread, which executes N5 lightweight tasks in turn. Because they are completed in a short time, they do not affect the timeliness of task response. Obviously, N5 is greater than or equal to 1.

  Implementation

Due to the length of the source code, not all code can be listed one by one. Here, the thread buffer pool is provided in the form of pseudocode in the Process of thread creation, destruction, task allocation, and task execution.

(1) Main cycle for allocating tasks in the thread pool (also a thread)

In addition to the task allocation algorithm, the algorithm for creating and destroying some threads is also included.

 

For (;;){

PThread = GetIdleThread (); // check the idle thread queue

If (pThread! = NULL ){

If (CheckNewTask () {// a new task exists.

TaskList tl;

GetTask (tl); // obtain a certain number of tasks

AddTaskToThread (pTask, tl); // transmits the task to the thread

Continue; // continue the loop

}

}

If (pThread = NULL & nThread <THREAD_MAX) // No idle thread

CreateNewThread (); // create a thread

Continue; // continue the loop

}

// No task to be processed or the maximum number of threads has been reached.

If (WaitForTaskOrThreadTimeout ()){

If (IncrIdleTime ()> IDLE_MAX) {// The system is idle, timing

// The system remains idle for a long time and destroys a certain number of Idle threads

DecrIdleThread ();

}

}

Else

Return 0; // thread termination

}

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.