Multitask array dispatching method based on thread poolTechnical field
The invention belongs to the computer engineering design field, related to a kind of multitask array dispatching method designed based on the worker thread pond.
Background technology
RTOS (Realtime operation system, real time operating system) is basis and the development platform of embedded application software, is a reliability and credible very high real-time kernel.RTOS wires up the resources such as CPU time, interruption, I/O, timer, leave API (the Application Program Interface of a standard of user for, and, according to the priority of each task, reasonably between different task, distribute CPU time application programming interfaces).
RTOS usually is arranged in embedded system and completes in real time various functions, and these embedded systems have that real-time is fine, system bulk is little usually, resource-constrained and there is no the characteristics such as graphical interfaces.Typical RTOS has: Vxworks, pSOS, Nucleus, eCos, uC/OS-II etc.
GUI (Graphic User Interface, graphical user interface) system realizes man-machine interaction in the mode of figure, scheduling mechanism is the core of GUI system, and driving and each application program of management GUI system, realize the shared use of each application program to cpu resource.High performance GUI scheduling mechanism not only takies cpu resource seldom, and can respond fast user's request.
Above-mentioned GUI scheduling mechanism does not refer to the task scheduling of operating system nucleus aspect, and refers to the scheduling mechanism between each application program with interface display.For example, start just enters the standby module, off-hook just enters dial module, off-hook is not pressed function key with regard to entered function menu module, thereby from function menu, select camera to enter photo module, if incoming call is now arranged, enter the voice call module, return to again photo module after the talk-through on-hook.
The GUI that current typical GUI system is Windows operating system, in embedded system, more common GUI system is Qtopia and MiniGUI.Below introduce respectively above-mentioned three GUI systems.
1.Windows CE (Control System, control system)
Windows CE is embedded OS, and complete gui interface and dispatching system is provided, almost completely consistent on interface with the Windows of PC version.
2.MiniGUI
MiniGUI, for the graphical interfaces back-up system of embedded device, cross operating system, belongs to a kind of " embedded graphic middleware " software product.In the development course of nearly 8 years, MiniGUI from one be only little technology in order to develop at the Linux Chinese display, develop into a mbedded GUI back-up system cross operating system, perfect.MiniGUI most desirably platform is (SuSE) Linux OS, also can support the RTOS such as uCOSII.
3.Qtopia
Qtopia is developed by Qt/Embedded, is the Qt version of embedded system.Because the GUI that Qt is the project such as KDE to be used supports storehouse, therefore there are many X Window programs based on Qt to be transplanted to easily on the Qtopia version.Qtopia is a C++ function library, and the control collection style that it provides has been continued to use the PC style.
The shortcoming of above-mentioned GUI system of the prior art is: the thread scheduling that the scheduling of above-mentioned GUI system is used kernel to provide fully, whether each application program is no matter need interface display to have thread independently to receive the scheduling of operating system.The operating system powerful based on bottom is done support, although the design of each application program has great dirigibility,, each application program is too heavy to the dependence of underlying operating system, usually need the support of the operating system of hardware and software platform, such as Windows or (SuSE) Linux OS.
The resource overhead of above-mentioned GUI system is huge, and the application demand of can not adaptive functions clear and definite low profile edge system is not generally supported the operating system of RTOS as bottom.In addition, for Embedded Application, also do not need complicated GUI system, too complicated GUI in fact can reduce the simplicity of user's operation.
Be a kind of slack relationships between each application program of above-mentioned GUI system, may occur two following problems while causing realizing a plurality of application program:
If 1 a plurality of application programs that will realize are as different function of application modules, requiring does not have too many connecting each other between these function of application modules.Once between these function of application modules, complicated contact be arranged, these function of application modules will inevitably produce conflict to the resource of system and the demand of equipment (such as Camera, internal memory), implement and are difficult to safeguard;
If 2 a plurality of application programs that will realize are as a single utility program functional module, bring huge maintenance difficulties can to a plurality of programmers' co-development, will cause a plurality of programmers to go to develop same module or a programmer completes a large-scale module.
And so shortcoming or deficiency appear in said system, be exactly mainly improper the causing of logical design of its dispatching method, therefore, we need the more rational multitask array dispatching method of a kind of logical design.
Summary of the invention
The queue data structure that provides a kind of worker thread pond built-in based on operating system and development language to provide and the multitask array dispatching method that designs are provided for the deficiencies in the prior art.This dispatching method is specially adapted to have a large amount of dissimilar tasks, and all tasks all need the sight that can immediately also process in order.The characteristics of this method are that it has good process or Thread Count scalability; Because it does not need for each task type creates the independently progress of work or a thread, so it can save system resource greatly.
To achieve these goals, the present invention has taked following technical scheme:
A kind of multitask array dispatching method based on thread pool comprises the following steps:
(1) be the task queue that every kind of task type creates a correspondence, and be designated as respectively TQ1, TQ2 ..., TQn;
(2) create one or two above task distributors, the reception task also distributes in corresponding task queue according to task type;
(3) initial work thread pool, the worker thread of idle condition enters sleep state;
(4), when the task distributor receives the task Ti of a certain type, find its corresponding task queue TQi, and judge whether the pending task of this task queue is empty;
(5) if the pending task of this task queue TQi is empty, task Ti is put in this task queue TQi, and carries out following operation:
A. search the worker thread of idle condition in the worker thread pond, if in the worker thread pond without the worker thread of idle condition, create a new worker thread, and it joined in the worker thread pond;
B. with the worker thread of the idle condition that finds or the newly-built worker thread Ti that executes the task;
(6) if the pending task of this task queue TQi is not empty, task Ti is placed on to the end of task queue TQi, wait task is carried out;
(7) after worker thread executes a task Ti, search the queue TQi that Ti is corresponding, then carry out following operation:
A. task Ti is removed from TQi;
If b. TQi is not empty, continue to carry out the task of team's head;
If c. TQi is empty, stop carrying out, worker thread returns the worker thread pond.
As of the present invention, further illustrate, above-described task distributor is independently process or a thread.
As of the present invention, further illustrate, above-described worker thread pond creates more than one worker thread, and worker thread is not directly related with task queue.
As of the present invention, further illustrate, above-described pending task comprises does not execute the task and is carrying out but uncompleted task.
As of the present invention, further illustrate, after above-described worker thread returns the worker thread pond, the worker thread pond will be destroyed this worker thread or this worker thread be distributed to other task queues and execute the task.
Advantage of the present invention:
1. in the present invention, create a plurality of task queues arranged, like this method just go for having a large amount of dissimilar tasks and all tasks all need can be immediately and the sight of processing in order.
2. in the present invention, the worker thread in task queue and worker thread pond does not have direct correlation, therefore, does not need, for each task type creates the independently progress of work or a thread, can effectively save greatly system resource.
3. in the present invention, the worker thread in the worker thread pond can create or destroy according to actual conditions, makes like this and adopts the dispatching system of this method to have good process or Thread Count scalability.
4. reasonable, feasible, the easy realization of logical design of the present invention.
The accompanying drawing explanation
Fig. 1 is the schematic diagram of dispatching method of the present invention.
Fig. 2 is the logical design process flow diagram of the task distributor in the present invention.
Fig. 3 is the logical design process flow diagram of the worker thread in the present invention.
Embodiment
Below in conjunction with drawings and Examples, the present invention is further described.
Embodiment:
As Figure 1-3, a kind of multitask array dispatching method based on thread pool comprises the following steps:
(1) be the task queue that every kind of task type creates a correspondence, and be designated as respectively TQ1, TQ2 ..., TQn;
(2) create one or two above task distributors, the reception task also distributes in corresponding task queue according to task type; Described task distributor is independently process or a thread;
(3) initial work thread pool, the worker thread of idle condition enters sleep state; Described worker thread pond creates more than one worker thread, and worker thread is not directly related with task queue;
(4), when the task distributor receives the task Ti of a certain type, find its corresponding task queue TQi, and judge whether the pending task of this task queue is empty; Described pending task comprises does not execute the task and is carrying out but uncompleted task;
(5) if the pending task of this task queue TQi is empty, task Ti is put in this task queue TQi, and carries out following operation:
A. search the worker thread of idle condition in the worker thread pond, if in the worker thread pond without the worker thread of idle condition, create a new worker thread, and it joined in the worker thread pond;
B. with the worker thread of the idle condition that finds or the newly-built worker thread Ti that executes the task;
(6) if the pending task of this task queue TQi is not empty, task Ti is placed on to the end of task queue TQi, wait task is carried out;
(7) after worker thread executes a task Ti, search the queue TQi that Ti is corresponding, then carry out following operation:
A. task Ti is removed from TQi;
If b. TQi is not empty, continue to carry out the task of team's head;
If c. TQi is empty, stop carrying out, worker thread returns the worker thread pond; After described worker thread returns the worker thread pond, the worker thread pond will be destroyed this worker thread or this worker thread be distributed to other task queues and execute the task.
The method goes for having a large amount of dissimilar tasks and all tasks all need the sight that can immediately also process in order, and do not need, for each task type creates the independently progress of work or a thread, can effectively save greatly system resource.