Task execution method and device, storage medium and electronic equipment
By binding threads with the core one -to -one core of the central processor, and using the mounting pointer to perform lightweight task scheduling, the problem of slower mission to perform the task of multi -core equipment is solved, and efficient task execution and low overhead scheduling are achieved.
Patent Information
- Application Number
- CN202411487589.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-23
- Publication Date
- 2025-01-24
AI Technical Summary
Existing multi-core devices perform tasks slowly and lack effective solutions.
By binding thread with the core one -to -one core of the central processor, each thread performs the task in the corresponding task pool in each thread to avoid switching the CPU usage right between threads, and packaging the execution function and the mounting pointer into a task. Use the mount pointer to perform lightweight task scheduling.
It improves the execution speed of tasks, realizes low-overhead scheduling of tasks on multi-cores, reduces the time required for scheduling tasks, and solves the problem of slow task execution speed of existing multi-core devices.
Smart Images

Figure CN119356819A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computers, and more specifically, to a task execution method and device, a storage medium, and an electronic device. Background Art
[0002] At present, most mainstream operating systems use a multi-threaded model to implement multitasking. Usually, multiple threads are created in the device to perform different tasks. At this time, each thread will take turns to obtain the right to use the central processing unit (CPU) and perform its own tasks respectively, so as to achieve macro concurrency. However, the switching between threads involves the conversion between kernel state and user state, which causes a relatively large time overhead and has a great impact on device performance. This time overhead cannot be adapted to some high-response, low-latency and multi-concurrency scenarios.
[0003] On the other hand, in order to improve device performance, increasing the number of CPU cores is the current development trend. Therefore, the multi-tasking execution of the device needs to adapt to the multi-core environment.
[0004] In view of the problem in the related technology that existing multi-core devices execute tasks slowly, no effective solution has been proposed so far. Summary of the invention
[0005] The embodiments of the present application provide a task execution method and device, a storage medium and an electronic device to at least solve the problem that existing multi-core devices execute tasks slowly.
[0006] According to an embodiment of the present application, a task execution method is provided, which is applied to a device, wherein a central processing unit of the device has multiple cores, each of the multiple cores is bound to a thread in an operating system of the device, and each thread is used to execute a task in a task pool corresponding to the thread, and the task includes: a mount pointer and an execution function; the mount pointer is used to reflect the state of the task, and the execution function has a function of the task to be executed; including: when it is determined through the mount pointer of the target task that the target task is in a state to be scheduled, determining a target task pool through the system, and adding the target task to the target task pool, wherein the task includes the target task; when there is a task to be executed in the task pool corresponding to a thread in the system, obtaining the task to be executed from the corresponding task pool through the thread, and executing the execution function of the task to be executed, wherein the task pool includes the target task pool.
[0007] In an exemplary embodiment, before the target task pool is determined by the system, the method also includes: creating a task module by the system according to one or more functions to be executed, so as to create the target task, wherein the target task includes a mount pointer and a target execution function, and the target execution function has the one or more functions to be executed; initializing the target task by calling an initialization task module by the system, wherein, in the case where the initialization task module initializes the target task for the first time, the initialization task module points the mount pointer of the target task to the target task, so that the target task is in a state to be scheduled; in the case where the initialization task module does not initialize the target task for the first time and the function to be executed is passed in when the initialization task module is called, the initialization task module uses the passed in function to be executed to update the target execution function of the target task.
[0008] In an exemplary embodiment, determining the target task pool through the system includes: determining whether there is a target identifier in the target task, wherein the target identifier is used to indicate the thread executing the target task; if the target identifier exists in the target task, determining the task pool corresponding to the thread indicated by the target identifier as the target task pool; if the target identifier does not exist in the target task and the difference in utilization of cores corresponding to multiple threads is greater than or equal to a first preset threshold or the difference in the number of tasks in the task pool corresponding to the multiple threads is greater than or equal to a second preset threshold, determining the target thread from the multiple threads according to the utilization of the cores corresponding to the multiple threads and the number of tasks in the task pool corresponding to the multiple threads, and determining the task pool corresponding to the target thread as the target task pool, wherein The multiple cores have a one-to-one correspondence with the multiple threads; when the target identifier does not exist in the target task, and the difference in utilization of the cores corresponding to the multiple threads is less than the first preset threshold, and the difference in the number of tasks in the task pool corresponding to the multiple threads is less than the second preset threshold, determine the value of the global variable and divide the value of the global variable by N, where N is equal to the number of the multiple threads; when the value of the global variable is divisible by N, determine the task pool corresponding to the Nth thread among the N threads as the target task pool, and increase the value of the global variable by one; when the value of the global variable cannot be divided by N, determine the remainder of the value of the global variable divided by N, and when the remainder is M, determine the task pool corresponding to the Mth thread among the N threads as the target task pool, and increase the value of the global variable by one.
[0009] In an exemplary embodiment, adding the target task to the target task pool includes: locking the target task pool, wherein, when the target task pool is in a locked state, objects other than the object that locks the target task pool are not allowed to operate on the target task pool; adding the target task to the target task pool; and unlocking the target task pool.
[0010] In an exemplary embodiment, adding the target task to the target task pool includes: determining whether the target task pool's to-be-executed pool allows the storage of tasks; if the target task pool's to-be-executed pool allows the storage of tasks, storing the target task in the target task pool's to-be-executed pool; if the target task pool's to-be-executed pool does not allow the storage of tasks, storing the target task in the waiting pool of the target task pool.
[0011] In an exemplary embodiment, the method also includes: when the time for executing the execution function of the task to be executed exceeds a preset time length, stopping the execution of the task to be executed; scheduling the task to be executed through the thread, re-determining the reference task pool from multiple task pools corresponding to multiple threads, and adding the task to be executed to the reference task pool.
[0012] In an exemplary embodiment, obtaining the to-be-executed tasks from the corresponding task pool through the thread includes: obtaining a task with the highest priority from the to-be-executed pool of the corresponding task pool through the thread; after obtaining a task with the highest priority from the to-be-executed pool of the corresponding task pool through the thread, the method further includes: deleting the task with the highest priority from the to-be-executed pool through the thread, and if there are tasks in the waiting pool of the task pool corresponding to the thread, adding the task with the highest priority in the waiting pool to the to-be-executed pool.
[0013] According to another embodiment of the embodiment of the present application, a task execution device is also provided, including: applied in a device, the central processing unit of the device has multiple cores, each of the multiple cores is bound to a thread in the operating system of the device, each thread is used to execute tasks in the task pool corresponding to the thread, and the task includes: a mount pointer and an execution function; the mount pointer is used to reflect the state of the task, and the execution function has a function of the task to be executed; including: an adding module, which is used to determine the target task pool through the system when it is determined that the target task is in a state to be scheduled through the mount pointer of the target task, and add the target task to the target task pool, wherein the task includes the target task; an execution module, which is used to obtain the task to be executed from the corresponding task pool through the thread when there is a task to be executed in the task pool corresponding to the thread in the system, and execute the execution function of the task to be executed, wherein the task pool includes the target task pool.
[0014] According to another embodiment of the present application, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above method embodiments when running.
[0015] According to another embodiment of the present application, an electronic device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.
[0016] According to another embodiment of the present application, a computer program product is provided. The computer program product includes a computer program. When the computer program is executed by a processor, the steps in any one of the above method embodiments are implemented.
[0017] Through this application, threads are bound to the cores of the central processing unit one-to-one, and each thread executes the tasks in the corresponding task pool, thereby avoiding the switching of CPU usage rights between threads and improving the execution speed of tasks. In addition, the execution function and the mount pointer are encapsulated into a task, and the task mount pointer is used to perform lightweight task scheduling on the task, thereby realizing low-overhead scheduling of tasks on multiple cores and reducing the time required for scheduling tasks. Thus, this application solves the problem that the existing multi-core devices execute tasks slowly. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0019] Figure 1 is a hardware structure block diagram of a server device of a task execution method according to an embodiment of the present application;
[0020] Figure 2 is a flowchart of a task execution method according to an embodiment of the present application;
[0021] Figure 3 is a structural block diagram of a task according to an embodiment of the present application;
[0022] Figure 4 is a structural block diagram of a task pool according to an embodiment of the present application;
[0023] Figure 5 is a schematic diagram of the operation logic of an initialization task module according to an embodiment of the present application;
[0024] Figure 6 is a schematic diagram of the operation logic of a scheduling task module according to an embodiment of the present application;
[0025] Figure 7 is a schematic diagram of the operation logic of an execution task module according to an embodiment of the present application;
[0026] Figure 8 is a structural block diagram of a task execution device according to an embodiment of the present application;
[0027] Fig. 9 It is a schematic diagram of the structure of an optional electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0028] The embodiments of the present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0029] It should be noted that the terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.
[0030] The task execution method embodiment provided in the embodiment of the present application can be executed in a server device or a similar computing device. Taking running on a server device as an example, Figure 1 1 is a hardware structure block diagram of a server device of a task execution method in an embodiment of the present application. Figure 1 As shown, the server device may include one or more ( Figure 1Only one is shown in the figure) processor 102 (processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data, wherein the server device may also include a transmission device 106 and an input / output device 108 for communication functions. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above server device. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations shown.
[0031] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the task execution method in the embodiment of the present application. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, to implement the above method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the server device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0032] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of the server device. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0033] In order to solve the above problems existing in the related art, a task execution method is provided in the present embodiment, which is applied to a device, wherein the central processor of the device has multiple cores, each of the multiple cores is bound to a thread in the operating system of the device, and each thread is used to execute a task in the task pool corresponding to the thread, and the task includes: a mount pointer and an execution function; the mount pointer is used to reflect the state of the task, and the execution function has a function to be executed by the task, such as Figure 2 As shown, the process includes the following steps S202-S204:
[0034] Step S202: when it is determined through the mount pointer of the target task that the target task is in a waiting-to-be-scheduled state, a target task pool is determined through the system, and the target task is added to the target task pool, wherein the task includes the target task;
[0035] It should be noted that a task is a task initiated by the system that needs to be executed. Figure 3 The structure diagram of the task is shown, and the structure of the task includes two parts: the mount pointer and the execution function. The execution function records the function that the task needs to execute and is the main body of the task. The mount pointer can be used to schedule the task, and the state of the task can also be identified according to the direction of the mount pointer, where the state of the task includes but is not limited to: the state to be scheduled, the state not allowed to be scheduled, and the state in the task pool. The present application encapsulates the execution function and the mount pointer into one task, and uses the mount pointer to perform lightweight task scheduling in the task pool, thereby realizing low-overhead scheduling of tasks on multiple cores.
[0036] Optionally, in the data structure of the task, except for the necessary execution function, the rest can be deleted as needed. Only one optional implementation example is shown here.
[0037] Step S204: when there are tasks to be executed in the task pool corresponding to the thread in the system, the thread obtains the tasks to be executed from the corresponding task pool and executes the execution function of the tasks to be executed, wherein the task pool includes the target task pool.
[0038] Optionally, the task pool can be a queue for storing tasks. Figure 4 The structure of a task pool is shown in the figure. A task pool includes a pending pool and a waiting pool. The pending pool is used to store the tasks to be executed in the scheduling. It can be implemented with a data structure that is convenient for storing data (for example, a circular queue). However, the data structure that is convenient for storing data generally has a limited number of storage. Therefore, the waiting pool is introduced. The function of the waiting pool is to store tasks after the pending pool is full. The implementation of the waiting pool requires the use of a data structure that can store data infinitely (for example, a single linked list).
[0039] It should be noted that the number of task pools depends on the number of threads in the worker thread pool (or the number of CPU cores). The worker thread pool contains N threads that actually execute tasks, where N is equal to the number of CPU cores. Each thread is bound one-to-one to a CPU core. The task pool is bound one-to-one to the worker thread, and the worker thread executes the tasks in the task pool by accessing it.
[0040] The above steps bind threads to the cores of the central processing unit one-to-one, and each thread executes the tasks in the corresponding task pool, thereby avoiding the switching of CPU usage rights between threads and improving the execution speed of tasks. In addition, the execution function and the mount pointer are encapsulated into a task, and the task mount pointer is used to perform lightweight task scheduling on the task, thereby realizing low-overhead scheduling of tasks on multiple cores and reducing the time required for scheduling tasks. The above steps solve the problem of slow execution of tasks in existing multi-core devices.
[0041] In an exemplary embodiment, before determining the target task pool by the system, the method further includes the following steps S11-S12:
[0042] Step S11: the system calls a task module according to one or more functions to be executed to create the target task, wherein the target task includes a mount pointer and a target execution function, and the target execution function includes the one or more functions to be executed;
[0043] It should be noted that the system can create a task by calling the task creation module and passing in the function that needs to be executed. The data structure of the created task is as follows Figure 3 As shown, the mount pointer of the newly created task points to null.
[0044] It should be noted that the above step S11 ensures that the task creation process is based on a specific execution function, so that the task has a clear execution target when it is created, that is, one or more functions to be executed. This not only improves the efficiency and accuracy of task creation, but also provides a basis for subsequent scheduling and execution.
[0045] In addition, by creating a task module, the system can encapsulate the function to be executed into the task structure to form a target execution function. This encapsulation method makes the task execution process clearer and easier to manage. When creating a task, the system can pre-allocate resources according to the needs of the function to be executed, avoiding the uncertainty of resource allocation during task scheduling and execution, thereby improving the overall performance of multi-tasking. The mount pointer of the newly created task points to null, which means that the task is in the initial state and has not been scheduled, which provides clear conditions and starting points for subsequent task scheduling.
[0046] Step S12: Initialize the target task by calling the initialization task module through the system, wherein, when the initialization task module initializes the target task for the first time, the initialization task module points the mount pointer of the target task to the target task so that the target task is in a state to be scheduled; when the initialization task module does not initialize the target task for the first time and a function to be executed is passed in when the initialization task module is called, the initialization task module uses the passed in function to be executed to update the target execution function of the target task.
[0047] It should be noted that the initialization task module is used to initialize the mount pointer and execution function. The initialization task module indicates state 2 (the aforementioned waiting-to-be-scheduled state) by pointing the mount pointer to the task itself. In this state, the task can be scheduled. If the mount pointer does not point to the task itself, it is state 1 (the state that scheduling is not allowed). Tasks in this state cannot be scheduled. At the same time, this module can also initialize the execution function and update the execution function of the task. Figure 5 As shown, the operation logic of the initialization task module is as follows:
[0048] S1: First determine whether the mount pointer needs to be initialized. If necessary, continue to step S2; if not, jump to step S3;
[0049] S2: The mount pointer points to itself, so that the task is in state 2, indicating that the task can be scheduled;
[0050] S3: Determine whether a new task function is passed in. If so, continue to step S4. If not, end.
[0051] S4: Update the execution function in the task structure using the new task function passed in.
[0052] In this embodiment, the above step S12, by initializing the task module, ensures that the task has the conditions for execution before scheduling, especially the correct pointing of the mount pointer and the update of the target execution function. This step is the key from the creation of the task to the schedulable object, which enhances the flexibility and adaptability of task scheduling.
[0053] In addition, the initialization task module points the mount pointer to the target task itself, and changes the task status from initial (mount pointer is empty) to pending scheduling (mount pointer points to the task itself), which is a necessary condition for the task to enter the scheduling queue. The state change ensures the accuracy and effectiveness of task scheduling. When a new function to be executed is passed in, the initialization task module can update the target execution function, which provides the possibility for dynamic adjustment of tasks, so that in the multi-task processing process, the execution content of the task can be flexibly adjusted according to real-time needs, improving the system's responsiveness and resource utilization efficiency. Ensure that each task is initialized before scheduling, avoid scheduling errors caused by unclear task status, and enhance the stability and robustness of the system.
[0054] It should be noted that the above steps S11 and S12 together ensure the accuracy and efficiency of the task in the creation and initialization stages, providing a solid foundation for subsequent multi-task asynchronous scheduling, thereby improving the performance and reliability of the multi-task processing system based on the multi-core platform as a whole.
[0055] In an exemplary embodiment, the above-mentioned determination of the target task pool by the system can be implemented by the following steps S21-S23:
[0056] Step S21: determining whether there is a target identifier in the target task, wherein the target identifier is used to indicate the thread executing the target task; if the target identifier exists in the target task, determining the task pool corresponding to the thread indicated by the target identifier as the target task pool;
[0057] Optionally, the data structure of the target task may further include a variable, and the variable is used to indicate the thread of the task to be executed, and then the thread executing the task can be determined by the value of the variable (ie the above-mentioned target identifier).
[0058] Step S22: when the target identifier does not exist in the target task, and the difference degree of utilization of the cores corresponding to the multiple threads is greater than or equal to the first preset threshold or the difference degree of the number of tasks in the task pool corresponding to the multiple threads is greater than or equal to the second preset threshold, determine the target thread from the multiple threads according to the utilization of the cores corresponding to the multiple threads and the number of tasks in the task pool corresponding to the multiple threads, and determine the task pool corresponding to the target thread as the target task pool, wherein the multiple cores have a one-to-one correspondence with the multiple threads;
[0059] Optionally, when the difference in utilization of the cores corresponding to any two threads among the multiple threads is less than the third preset threshold, it means that the difference in utilization of the cores corresponding to the multiple threads is less than the first preset threshold.
[0060] Optionally, when the difference in the number of tasks in the task pool corresponding to any two threads among the multiple threads is less than the fourth preset threshold, it means that the difference in the number of tasks in the task pool corresponding to the multiple threads is less than the second preset threshold.
[0061] Optionally, a reference value can be calculated based on the utilization rate of the core corresponding to each thread and the number of tasks in the task pool corresponding to the thread, and the reference value is used to reflect the time required for the thread to complete all tasks in the corresponding task pool, wherein the smaller the reference value, the shorter the time required to complete all tasks in the corresponding task pool. Then, a thread with the smallest corresponding reference value among the multiple threads can be determined as the target thread.
[0062] Step S23: When the target identifier does not exist in the target task, and the difference in utilization of cores corresponding to multiple threads is less than the first preset threshold, and the difference in the number of tasks in the task pool corresponding to multiple threads is less than the second preset threshold, determine the value of the global variable and divide the value of the global variable by N, where N is equal to the number of the multiple threads; when the value of the global variable is divisible by N, determine the task pool corresponding to the Nth thread among the N threads as the target task pool, and increase the value of the global variable by one; when the value of the global variable cannot be divided by N, determine the remainder of the value of the global variable divided by N, and when the remainder is M, determine the task pool corresponding to the Mth thread among the N threads as the target task pool, and increase the value of the global variable by one.
[0063] It should be noted that if the difference in the utilization of the cores corresponding to multiple threads is less than the first preset threshold, and the difference in the number of tasks in the task pool corresponding to multiple threads is less than the second preset threshold, it means that the multiple threads are now in a load-balanced state, and a polling strategy can be used to schedule tasks.
[0064] As an optional example, the above steps S21-S23 can be implemented by the system calling the scheduling task module, which is used to schedule tasks. Each task can initiate scheduling using this module. For a better understanding, when the scheduling task module uses a polling strategy to schedule tasks, the specific scheduling steps can be referred to Figure 6 As shown, specifically:
[0065] Step S1: Determine whether the task is in a state 2 where it can be scheduled. If yes, jump to step S2. If no, end.
[0066] Step S2: Determine which task pool the task should be placed in based on the polling strategy. Specifically, you can maintain a global variable, divide the value of the global variable by the number of task pools, and determine which task pool the task should be placed in based on the remainder obtained by calculation; after the scheduling is completed, add 1 to the global variable. Here is an example, if there are 10 task pools and the value of the global variable is 95, then the remainder is 5, and the task should be placed in the 5th task pool. After scheduling, the global variable is added by 1, which is 96. Then the next time the task is scheduled, the task will be placed in the 6th task pool.
[0067] It should be noted that, in this embodiment, by checking the target identifier in the target task, the system can quickly identify the most suitable thread and its corresponding task pool to perform a specific task, thereby improving scheduling efficiency. This embodiment also provides an optional load balancing strategy, which can dynamically adjust task allocation according to the core utilization and the difference in the number of tasks in the task pool, ensuring that tasks can be executed on threads with lighter loads, thereby achieving more efficient load balancing. This strategy helps to prevent some threads from being overloaded while other thread resources are idle, ensuring that the resources of multi-core devices are more reasonably utilized, and improving overall processing power and response speed. In addition, when the core utilization of multiple threads and the difference in the number of tasks in the task pool are both less than a preset threshold, it indicates that the system is in a relatively balanced operating state. At this time, by maintaining a global variable and using a polling method to assign tasks to different threads, it is ensured that all threads have the opportunity to perform tasks, further ensuring the fairness and stability of the system.
[0068] In addition, the above method not only simplifies the implementation of multi-task scheduling and improves the efficiency and flexibility of scheduling, but also ensures the optimal utilization of multi-core platform resources through dynamic load balancing and fair task allocation strategies, reduces resource conflicts, improves the overall performance and security of the system, and fully adapts to the multi-task processing requirements of high-performance devices.
[0069] In an exemplary embodiment, the above-mentioned adding the target task to the target task pool includes the following steps S31-S33:
[0070] Step S31: Locking the target task pool, wherein when the target task pool is in a locked state, objects other than the object that locks the target task pool are not allowed to operate on the target task pool;
[0071] Step S32: adding the target task to the target task pool;
[0072] Step S33: Unlock the target task pool.
[0073] It should be noted that the above steps S31-S33 can be implemented by calling the system scheduling task module.
[0074] In this embodiment, locking the task pool is a key step in ensuring data consistency and thread safety in the multi-core asynchronous scheduling method. In a multi-tasking and multi-threading environment, especially on a multi-core processor platform, the locking mechanism can bring the following significant advantages:
[0075] 1. Prevent data competition: On multiple threads or multiple CPU cores, the task pool is a shared resource. If it is not locked, multiple threads or cores may access the task pool at the same time, resulting in data competition, that is, two or more threads try to modify the same data item at the same time. Locking can ensure that only one thread can modify the task pool at a time, thereby avoiding data competition and the resulting data inconsistency problems.
[0076] 2. Maintain data consistency: Locking ensures that modifications to the task pool are atomic, that is, this series of operations will not be interrupted by other threads or cores. For example, when a thread inserts a new task into the task pool, locking can ensure that this operation will not be interrupted by another thread trying to read the task pool, thereby maintaining data consistency.
[0077] 3. Improve system reliability: In a concurrent environment, unlocked data operations may cause system instability or crash. Locking improves the overall reliability and robustness of the system by preventing unsynchronized access.
[0078] That is, after determining the task pool to be placed in, the task pool is locked to ensure data security under multi-core.
[0079] In an exemplary embodiment, the above-mentioned adding the target task to the target task pool includes the following steps S41-S43:
[0080] Step S4 1: Determine whether the pending execution pool of the target task pool allows the storage of tasks;
[0081] Step S42: if the to-be-executed pool of the target task pool allows for storage of tasks, storing the target task in the to-be-executed pool of the target task pool;
[0082] Step S43: When the to-be-executed pool of the target task pool is not allowed to store tasks, the target task is stored in the waiting pool of the target task pool.
[0083] It should be noted that the above steps S41-S43 can be implemented by calling the system scheduling task module. For a better understanding, the following is combined with Figure 6To explain in detail, specifically, the scheduling task module needs to determine whether there are any vacancies in the pending execution pool of the task pool (for example, determine whether the circular queue is full). If there are any vacancies, the task is placed in the pending execution pool. In this embodiment, the task is placed at the end of the circular queue, and the mount pointer of the task is pointed to the corresponding position of the circular queue, indicating state 3, which means that the task is already in the queue. If there are no vacancies, the task is placed in the waiting pool. In this embodiment, the task is inserted at the end of the linked list.
[0084] It should be noted that, in this embodiment, the combination of the pending execution pool (usually a circular queue) and the waiting pool (usually a linked list) makes task scheduling more flexible. The pending execution pool is used to store tasks that are about to be executed, and the waiting pool is used to store tasks when the pending execution pool is full. This design avoids task delays caused by the capacity limitation of the pending execution pool and ensures that tasks can be processed in a timely manner even under high load conditions.
[0085] In addition, when the waiting pool is full, the task is placed in the waiting pool instead of switching between threads or fibers, reducing the overhead of context switching. In addition, by pointing the task's mount pointer to the position in the queue or linked list, lightweight task status tracking can be achieved, further reducing the system's management overhead.
[0086] In an exemplary embodiment, the method further comprises the following steps S51-S52:
[0087] Step S51: when the execution time of the execution function of the task to be executed exceeds a preset time length, stop executing the task to be executed;
[0088] It should be noted that after the thread stops executing the task, it will directly remove the task from the current task pool.
[0089] Step S52: Scheduling the tasks to be executed through the threads, re-determining a reference task pool from a plurality of task pools corresponding to a plurality of threads, and adding the tasks to be executed to the reference task pool.
[0090] It should be noted that, in this embodiment, not only the system can call the scheduling task module to schedule the task after creating the task, but also the thread can call the scheduling task module to schedule the task when the task execution times out.
[0091] It should be noted that the above steps, by monitoring the task execution time and rescheduling in the event of a timeout, can effectively prevent certain tasks from being executed for a long time on a specific core, resulting in excessive concentration of resources and affecting the overall performance and response speed of the system. Task rescheduling can ensure that all cores can be fully utilized, avoiding the situation where a single core is overused while other cores are idle, thereby improving the utilization of multi-core platforms. In addition, by dynamically adjusting the position of tasks in different task pools, the execution order of tasks can be optimized, the waiting time between tasks can be reduced, and the concurrent processing capability of the system can be improved.
[0092] In an exemplary embodiment, the acquiring the to-be-executed task from the corresponding task pool by the thread includes: acquiring a task with the highest priority from the to-be-executed pool of the corresponding task pool by the thread;
[0093] It should be noted that, when the waiting pool is a circular queue, the task with the highest priority in the waiting pool is the task at the head of the circular queue.
[0094] In an exemplary embodiment, after obtaining a task with the highest priority from the to-be-executed pool of the corresponding task pool through the thread, the method further includes: deleting the task with the highest priority from the to-be-executed pool through the thread, and if there is a task in the waiting pool of the task pool corresponding to the thread, adding the task with the highest priority in the waiting pool to the to-be-executed pool.
[0095] It should be noted that, when the waiting pool is a single linked list, the task with the highest priority in the waiting pool is the task at the head of the queue in the linked list.
[0096] For a better understanding, the following is a detailed description. Each thread in the worker thread pool executes the task in the task pool by calling the execution task module, such as Figure 7 As shown, the specific steps of an execution process in the execution task module are as follows:
[0097] S 1: First, determine whether there is a task to be executed in the waiting pool. If yes, continue to execute. If not, end.
[0098] S2: Take a task from the waiting pool, which in this embodiment is to take the task at the head of the queue from the circular queue; S3: Point the task status pointer to itself, and the task status changes to status 2, indicating that it can be scheduled again;
[0099] S4: execution function to execute the task;
[0100] S5: Determine whether the execution time of this task is too long. If it is too long, schedule this task again to wait for the next execution to prevent the execution process from being blocked;
[0101] S6: If the task execution time meets the conditions, it is determined whether the waiting pool is empty. If it is empty, the execution is completed once, and the process jumps back to step S1 to loop. If the waiting pool is not empty, the task in the waiting pool is taken and placed in the empty space in the waiting pool. In this embodiment, the task at the head of the queue in the linked list is taken out and placed at the end of the queue in the circular queue. After completion, the process jumps back to step S1 to loop.
[0102] In an exemplary embodiment, the system also has an intelligent task priority adjustment module to improve the efficiency and response speed of task scheduling. The intelligent task priority adjustment module can dynamically adjust the position of tasks of different types or priorities in the task pool, thereby optimizing the multi-tasking process.
[0103] Specifically, the main function of the intelligent task priority adjustment module is to dynamically adjust the position of tasks in the waiting pool and waiting pool after the tasks enter the task pool according to the characteristics of the tasks (such as task type, urgency of task execution, and demand for system resources by the tasks). This adjustment helps ensure that high-priority tasks can be scheduled and executed faster, thereby improving the overall system response speed and efficiency.
[0104] It should be noted that in order to implement this module, a priority field needs to be added to the task data structure to identify the priority of the task. The priority field can be an integer, and the lower the value, the higher the priority.
[0105] In the waiting pool (circular queue), you can use algorithms such as insertion sort or quick sort to reorder the tasks in the queue according to their priorities. When a new task is scheduled into the waiting pool, it will be inserted into the appropriate position according to its priority, rather than simply added to the end of the queue.
[0106] In the waiting pool (single linked list), you can traverse the linked list to find the nearest node with a lower priority than the current task, and then insert the current task before the node. This step can be performed when the task enters the waiting pool, or you can set a periodic adjustment mechanism to regularly check and adjust the order of tasks in the waiting pool.
[0107] Specifically, when a task enters the pending pool or the waiting pool, the intelligent task priority adjustment module first reads the priority field of the task. Based on the read priority, the module determines the insertion position of the task in the queue. For the pending pool, a sorting algorithm is used to ensure that tasks are arranged in order of priority; for the waiting pool, the linked list is traversed to find the appropriate position and insert the task. When the execution task module is running, the task with the highest priority in the pending pool is executed first to achieve efficient response. When the task in the waiting pool is scheduled to the pending pool, the intelligent priority adjustment is performed again to ensure that the task is in the optimal position in the pending pool.
[0108] It should be noted that through intelligent task priority adjustment, high-priority tasks can be scheduled and executed in a shorter time, greatly improving the response speed of the system, which is particularly suitable for scenarios with high real-time requirements.
[0109] It should be noted that the present application provides a multi-task asynchronous scheduling method based on a multi-core platform, which is suitable for devices with multiple physical cores and high performance requirements (such as storage devices, etc.). There are three main data structures contained in this method, namely: task, thread pool, and task pool. A task consists of a mount pointer and an execution function. A task pool corresponds to a thread in the thread pool, and a thread is bound to a CPU core (that is, there are N CPU cores, and there are N threads in the thread pool, which are bound one-to-one with the CPU core; correspondingly, there are N task pools, which correspond one-to-one with N threads). The task pool is used to store tasks, and threads are used to execute tasks in the corresponding task pool.
[0110] This application contains four main modules, namely: task creation module, task initialization module, task scheduling module, and task execution module. This application completes the task production by calling the task creation module, task initialization module, and task scheduling module by the system; and completes the task execution by calling the task execution module by the thread in the working thread pool as a consumer.
[0111] The method of the present application realizes an asynchronous calling method for multiple tasks that can be used on a multi-core platform. Compared with multi-threading technology, it is simpler and easier to implement, and can meet the requirements of high-performance devices. Compared with the fiber technology that is currently the most commonly used technology for high-performance devices, the present application can not only replace the fiber to meet the high-performance requirements of high-response and low-latency devices, but also avoid the defect that the current fiber synchronization mechanism cannot utilize the multi-core platform. The present application uses an asynchronous scheduling method for multiple tasks to ensure the stability and robustness of the program while utilizing the multi-core platform to quickly achieve the requirements of multi-task functions, and avoids the risks and consumption of multi-thread concurrency through thread locks. By adopting this method, programmers can fully utilize the advantages of the multi-core platform with low overhead, and the purpose of multi-tasking can be achieved according to the technical solution of the above method.
[0112] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present application.
[0113] In this embodiment, a task execution device is also provided for implementing the above embodiments and preferred implementations, which have been described and will not be repeated. As used below, the term "module" can be software and / or software that implements a predetermined function.
[0114] Although the modules described in the following embodiments are preferably implemented in software, implementation in hardware, or a combination of software and hardware is also possible and conceivable.
[0115] Figure 8 It is a structural block diagram of a task execution device according to an embodiment of the present application, the device is applied to a device, the central processor of the device has multiple cores, each of the multiple cores is bound to a thread in the operating system of the device, each thread is used to execute a task in the task pool corresponding to the thread, and the task includes: a mount pointer and an execution function; the mount pointer is used to reflect the state of the task, and the execution function has a function to be executed by the task, including:
[0116] An adding module 802 is used to determine a target task pool through a system and add the target task to the target task pool when it is determined through a mount pointer of the target task that the target task is in a to-be-scheduled state, wherein the task includes the target task;
[0117] The execution module 804 is used to obtain the task to be executed from the corresponding task pool through the thread when there is a task to be executed in the task pool corresponding to the thread in the system, and execute the execution function of the task to be executed, wherein the task pool includes the target task pool.
[0118] The above device binds threads to the cores of the central processing unit one-to-one, and each thread executes the tasks in the corresponding task pool, thereby avoiding the switching of CPU usage rights between threads and improving the execution speed of tasks. In addition, the execution function and the mount pointer are encapsulated into a task, and the task mount pointer is used to perform lightweight task scheduling on the task, thereby realizing low-overhead scheduling of tasks on multiple cores and reducing the time required for scheduling tasks. This solves the problem of slow execution of tasks on existing multi-core devices.
[0119] In an exemplary embodiment, the device also includes: a processing module, which is used to create a task module by calling the system according to one or more functions to be executed to create the target task before determining the target task pool through the system, wherein the target task includes a mount pointer and a target execution function, and the target execution function has the one or more functions to be executed; calling the initialization task module through the system to initialize the target task, wherein, in the case where the initialization task module initializes the target task for the first time, the initialization task module points the mount pointer of the target task to the target task to put the target task in a state to be scheduled; in the case where the initialization task module does not initialize the target task for the first time and the function to be executed is passed in when the initialization task module is called, the initialization task module uses the passed in function to be executed to update the target execution function of the target task.
[0120] In an exemplary embodiment, the adding module 802 is further used to determine whether there is a target identifier in the target task, wherein the target identifier is used to indicate the thread that executes the target task; when the target identifier exists in the target task, the task pool corresponding to the thread indicated by the target identifier is determined as the target task pool; when the target identifier does not exist in the target task and the difference in the utilization of the cores corresponding to the multiple threads is greater than or equal to a first preset threshold or the difference in the number of tasks in the task pools corresponding to the multiple threads is greater than or equal to a second preset threshold, the target thread is determined from the multiple threads according to the utilization of the cores corresponding to the multiple threads and the number of tasks in the task pools corresponding to the multiple threads, and the task pool corresponding to the target thread is determined as the target task pool, wherein the The multiple cores have a one-to-one correspondence with the multiple threads; when the target identifier does not exist in the target task, and the difference in utilization of the cores corresponding to the multiple threads is less than the first preset threshold, and the difference in the number of tasks in the task pool corresponding to the multiple threads is less than the second preset threshold, determine the value of the global variable and divide the value of the global variable by N, where N is equal to the number of the multiple threads; when the value of the global variable is divisible by N, determine the task pool corresponding to the Nth thread among the N threads as the target task pool, and increase the value of the global variable by one; when the value of the global variable cannot be divided by N, determine the remainder of the value of the global variable divided by N, and when the remainder is M, determine the task pool corresponding to the Mth thread among the N threads as the target task pool, and increase the value of the global variable by one.
[0121] In an exemplary embodiment, the adding module 802 is also used to lock the target task pool, wherein, when the target task pool is in a locked state, objects other than the object that locks the target task pool are not allowed to operate on the target task pool; add the target task to the target task pool; and unlock the target task pool.
[0122] In an exemplary embodiment, the adding module 802 is also used to determine whether the storage of tasks in the to-be-executed pool of the target task pool is allowed; if the storage of tasks in the to-be-executed pool of the target task pool is allowed, the target task is stored in the to-be-executed pool of the target task pool; if the storage of tasks in the to-be-executed pool of the target task pool is not allowed, the target task is stored in the waiting pool of the target task pool.
[0123] In an exemplary embodiment, the execution module 804 is also used to stop executing the task to be executed when the time for executing the execution function of the task to be executed exceeds a preset time length; schedule the task to be executed through the thread, re-determine the reference task pool from multiple task pools corresponding to multiple threads, and add the task to be executed to the reference task pool.
[0124] In an exemplary embodiment, the execution module 804 is also used to obtain a task with the highest priority from the to-be-executed pool of the corresponding task pool through the thread; delete the task with the highest priority from the to-be-executed pool through the thread, and if there is a task in the waiting pool of the task pool corresponding to the thread, add the task with the highest priority in the waiting pool to the to-be-executed pool.
[0125] It should be noted that the above modules can be implemented by software or hardware. For the latter, it can be implemented in the following ways, but not limited to: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.
[0126] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above method embodiments when running.
[0127] Optionally, in this embodiment, the computer program may be configured to perform the following steps through the computer program: S1, in a case where it is determined through the mount pointer of the target task that the target task is in a to-be-scheduled state, determining a target task pool through the system, and adding the target task to the target task pool, wherein the task includes the target task;
[0128] S2, when there are tasks to be executed in the task pool corresponding to the thread in the system, obtain the tasks to be executed from the corresponding task pool through the thread, and execute the execution function of the tasks to be executed, wherein the task pool includes the target task pool.
[0129] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk.
[0130] The embodiment of the present application also provides an electronic device, such as Fig. 9As shown, the electronic device includes a memory 908 and a processor 904. The memory 908 stores a computer program. The processor 904 is configured to execute the steps in any of the above method embodiments through the computer program.
[0131] Optionally, in this embodiment, the processor 904 may be configured to perform the following steps through a computer program:
[0132] S1, when it is determined through the mount pointer of the target task that the target task is in a waiting-to-be-scheduled state, a target task pool is determined through the system, and the target task is added to the target task pool, wherein the task includes the target task;
[0133] S2, when there are tasks to be executed in the task pool corresponding to the thread in the system, obtain the tasks to be executed from the corresponding task pool through the thread, and execute the execution function of the tasks to be executed, wherein the task pool includes the target task pool.
[0134] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.
[0135] Alternatively, a person skilled in the art may understand that: Fig. 9 The structure shown is for illustration only. Fig. 9 The structure of the electronic device is not limited. For example, the electronic device may also include Fig. 9 More or fewer components (such as network interfaces, etc.) as shown in, or with Fig. 9 Different configurations shown.
[0136] Among them, the memory 908 can be used to store software programs and modules, such as the program instructions / modules corresponding to the task execution method and the task execution device in the embodiment of the present application. The processor 904 executes various functional applications and data processing by running the software programs and modules stored in the memory 908, that is, realizing the above-mentioned task execution method. The memory 908 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 908 may further include a memory remotely located relative to the processor 904, and these remote memories may be connected to the terminal via a network. Examples of the above-mentioned networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof. Among them, the memory 908 can be specifically, but not limited to, used to store information such as system configuration files. As an example, such as Fig. 9As shown, the memory 908 may include but is not limited to the adding module 802 and the executing module 804 in the task executing device. In addition, it may also include but is not limited to other module units in the task executing device, which will not be described in detail in this example.
[0137] Optionally, the transmission device 906 is used to receive or send data via a network. Specific examples of the above-mentioned network may include a wired network and a wireless network. In one example, the transmission device 906 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices and routers via a network cable so as to communicate with the Internet or a local area network. In one example, the transmission device 906 is a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0138] In addition, the electronic device mentioned above further includes: a display 908; and a connection bus 910, which is used to connect various module components in the electronic device mentioned above.
[0139] An embodiment of the present application further provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps in any one of the above method embodiments are implemented.
[0140] An embodiment of the present application further provides another computer program product, including a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps in any of the above method embodiments are implemented.
[0141] The embodiments of the present application also provide a computer program, which includes computer instructions, which are stored in a computer-readable storage medium; a processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device performs the steps of any one of the above method embodiments.
[0142] Obviously, those skilled in the art should understand that the above modules or steps of the present application can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order from that herein, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present application is not limited to any specific combination of hardware and software.
[0143] The above description is only the preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the principles of the present application shall be included in the protection scope of the present application.
Claims
1. A task execution method, characterized in that: Applied in a device, the central processor of the device has multiple cores, each of the multiple cores is bound to a thread in the operating system of the device, each thread is used to execute a task in a task pool corresponding to the thread, and the task includes: a mount pointer and an execution function; the mount pointer is used to reflect the state of the task, and the execution function has a function to be executed by the task; include: When it is determined through the mount pointer of the target task that the target task is in a state to be scheduled, a target task pool is determined through the system, and the target task is added to the target task pool, wherein the task includes the target task; when there are tasks to be executed in the task pool corresponding to the thread in the system, the task to be executed is obtained from the corresponding task pool through the thread, and an execution function of the task to be executed is executed, wherein the task pool includes the target task pool.
2. The method according to claim 1, characterized in that Before determining the target task pool by the system, the method further includes: The system calls a task module according to one or more functions to be executed to create the target task, wherein the target task includes a mount pointer and a target execution function, and the target execution function includes the one or more functions to be executed; The target task is initialized by calling the initialization task module through the system, wherein, when the initialization task module initializes the target task for the first time, the initialization task module points the mount pointer of the target task to the target task so that the target task is in a state to be scheduled; when the initialization task module does not initialize the target task for the first time and a function to be executed is passed in when calling the initialization task module, the initialization task module uses the passed in function to be executed to update the target execution function of the target task.
3. The method according to claim 1, characterized in that Determining the target task pool through the system includes: Determine whether there is a target identifier in the target task, wherein the target identifier is used to indicate the thread that executes the target task; if the target identifier exists in the target task, determine the task pool corresponding to the thread indicated by the target identifier as the target task pool; In the case where the target identifier does not exist in the target task, and the difference degree of utilization of the cores corresponding to the multiple threads is greater than or equal to the first preset threshold or the difference degree of the number of tasks in the task pool corresponding to the multiple threads is greater than or equal to the second preset threshold, determining the target thread from the multiple threads according to the utilization of the cores corresponding to the multiple threads and the number of tasks in the task pool corresponding to the multiple threads, and determining the task pool corresponding to the target thread as the target task pool, wherein the multiple cores have a one-to-one correspondence with the multiple threads; When the target identifier does not exist in the target task, and the difference in utilization of cores corresponding to multiple threads is less than the first preset threshold, and the difference in the number of tasks in the task pool corresponding to multiple threads is less than the second preset threshold, determine the value of the global variable and divide the value of the global variable by N, where N is equal to the number of the multiple threads; when the value of the global variable is divisible by N, determine the task pool corresponding to the Nth thread among the N threads as the target task pool, and increase the value of the global variable by one; when the value of the global variable cannot be divided by N, determine the remainder of the value of the global variable divided by N, and when the remainder is M, determine the task pool corresponding to the Mth thread among the N threads as the target task pool, and increase the value of the global variable by one.
4. The method according to claim 1, characterized in that: The adding the target task to the target task pool includes: Locking the target task pool, wherein, when the target task pool is in a locked state, objects other than the object that locks the target task pool are not allowed to operate on the target task pool; Adding the target task to the target task pool; Unlock the target task pool.
5. The method according to any one of claims 1 to 4, characterized in that The adding the target task to the target task pool includes: Determine whether the task is allowed to be stored in the pending execution pool of the target task pool; If the target task pool allows the storage of tasks, storing the target task in the target task pool; When the to-be-executed pool of the target task pool does not allow the storage of tasks, the target task is stored in the waiting pool of the target task pool.
6. The method according to claim 1, characterized in that The method further comprises: When the execution time of the execution function of the task to be executed exceeds a preset time length, stopping the execution of the task to be executed; The tasks to be executed are scheduled by the threads, a reference task pool is re-determined from a plurality of task pools corresponding to a plurality of threads, and the tasks to be executed are added to the reference task pool.
7. The method according to claim 1, characterized in that The obtaining of the to-be-executed task from the corresponding task pool by the thread includes: obtaining a task with the highest priority from the to-be-executed pool of the corresponding task pool by the thread; After obtaining a task with the highest priority from the to-be-executed pool of the corresponding task pool through the thread, the method further includes: deleting the task with the highest priority from the to-be-executed pool through the thread, and adding the task with the highest priority in the waiting pool to the to-be-executed pool if there is a task in the waiting pool of the task pool corresponding to the thread.
8. A task execution device, characterized in that: Applied in a device, the central processor of the device has multiple cores, each of the multiple cores is bound to a thread in the operating system of the device, each thread is used to execute a task in a task pool corresponding to the thread, and the task includes: a mount pointer and an execution function; the mount pointer is used to reflect the state of the task, and the execution function has a function to be executed by the task; include: An adding module, for determining a target task pool through a system and adding the target task to the target task pool when it is determined through a mount pointer of the target task that the target task is in a to-be-scheduled state, wherein the task includes the target task; An execution module is used to obtain the task to be executed from the corresponding task pool through the thread when there is a task to be executed in the task pool corresponding to the thread in the system, and execute the execution function of the task to be executed, wherein the task pool includes the target task pool.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program implements the steps of the method described in any one of claims 1 to 7 when executed by a processor.
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method described in any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Multithread system performance enhancement method and device
CN107301087A
Single-thread multi-task execution method and device, equipment and medium
CN115576671A
Task execution method and system for thread pool
CN115705242A