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 } |