Task scheduling method, electronic equipment and storage medium

By assigning tasks to local queues of multiple worker threads in multi-task high concurrency scenarios, and using monitoring threads and task springboard mechanisms for load balancing scheduling, the problems of large thread synchronization overhead, task blocking and load unbalanced are solved, and efficient task scheduling and system stability are achieved.

CN120144252APending Publication Date: 2025-06-13安徽蔚来智驾科技有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510220959.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-27
Publication Date
2025-06-13

AI Technical Summary

Technical Problem

In scenarios where multitasking high concurrent execution is performed, the existing task scheduling methods have problems such as high thread synchronization overhead, thread task blocking and load imbalance.

Method used

A task scheduling method is proposed. By assigning tasks to local queues of multiple worker threads, and using monitoring threads to perform task transfer and load balancing, the task springboard mechanism is used to transfer target tasks to local queues of idle worker threads, realizing load balancing scheduling of tasks.

Benefits of technology

It effectively reduces the synchronization overhead between threads, reduces the blockage of thread tasks, and realizes the balance of thread load, improving the efficiency of task scheduling and system stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120144252A_ABST
    Figure CN120144252A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of computers, particularly provides a task scheduling method, electronic equipment and a storage medium, and aims to solve the problems of how to reduce thread synchronization overhead and task blockage during task scheduling and how to realize load balancing. The method provided by the invention comprises the following steps: assigning N tasks to a local queue of M working threads; adopting M working threads to schedule and execute tasks in respective local queues in parallel; a monitoring thread is adopted to transfer a target task in a local queue to a task springboard, and the target task is scheduled and executed by an idle working thread; assigning the task in the task springboard to a working thread which executes the task most frequently by adopting a monitoring thread; the target task is a to-be-executed task when the working thread is in the target state or a to-be-executed task which cannot be scheduled, and the target state is that the load of the working thread is the highest and the load of the M working threads is unbalanced. By means of the method, the synchronization overhead and task blocking of the threads can be effectively reduced, and load balancing is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and particularly to a task scheduling method, an electronic device, and a storage medium. Background Art

[0002] When an application runs on an electronic device, a large number of tasks are generated, and these tasks are executed in parallel by multiple worker threads. In order to improve the running efficiency and performance of the application, it is usually necessary to schedule the tasks. Currently, conventional task scheduling methods include a global queue scheduling method and a local queue scheduling method. For example, the global queue scheduling method can include the scheduling mechanism of ROS2 (Robot Operating System 2), the classic scheduling mechanism of Cyber RT (Cyber Run Time), etc., and the local queue scheduling method can include the choreography scheduling mechanism of Cyber RT, etc. Cyber RT is a key part of the Apollo autonomous driving framework, and Cyber RT is mainly responsible for tasks such as real-time scheduling of tasks, high efficiency and real-time of data communication. However, the global queue scheduling method and the local queue scheduling method have the following disadvantages:

[0003] In the global queue scheduling method, all tasks are placed in the global queue, and multiple worker threads obtain tasks from the global queue and execute them, which results in a large synchronization overhead between threads in the case of high task pressure; in the local queue scheduling method, specific tasks are firmly bound to specific worker threads. Although the synchronization overhead between threads is reduced, problems such as uneven load between threads often occur, or a certain worker thread cannot be scheduled, which will block the execution of other tasks.

[0004] Correspondingly, a new technical solution is needed in this field to solve the above problems. Summary of the Invention

[0005] In order to overcome the above defects, this application is proposed to solve or at least partially solve the following technical problems: how to reduce the synchronization overhead of threads, reduce task blocking of threads, and achieve load balancing of threads when performing task scheduling in a scenario of multi-task high-concurrency execution.

[0006] In a first aspect, a task scheduling method is provided, and the method includes:

[0007] Assign N tasks to the local queues of M worker threads, where N≥1 and M≥1;

[0008] Use the M worker threads to schedule and execute the tasks in their respective local queues in parallel;

[0009] For each of the working threads, a monitoring thread is used to obtain the target tasks in the local queue of the working thread and transfer the target tasks to a preset task springboard. The tasks in the task springboard are scheduled and executed by idle working threads;

[0010] For each task in the task springboard, the monitoring thread is used to obtain the number of times the task is executed, and when the number reaches a set value, the task is assigned to the local queue of the target thread, where the target thread is the working thread that has executed the task the most times;

[0011] Among them, the target tasks include first tasks and / or second tasks;

[0012] The first task is a to-be-executed task when the working thread is in a target state, where the target state is that the working thread has the highest load and there is load imbalance among the M working threads;

[0013] The second task is a to-be-executed task that cannot be scheduled by the working thread.

[0014] In a technical solution of the above task scheduling method, the method further includes the monitoring thread determining that there is load imbalance among the M working threads in the following manner:

[0015] Detect the load of each of the M working threads, and obtain the first working thread with the highest load and the second working thread with the lowest load;

[0016] Obtain the load difference between the first working thread and the second working thread; if the load difference is greater than a set threshold, then there is load imbalance among the M working threads.

[0017] In a technical solution of the above task scheduling method, the load of the working thread is the CPU resources consumed when the CPU runs the working thread.

[0018] In a technical solution of the above task scheduling method, the method further includes the monitoring thread obtaining the first task in the local queue in the following manner: randomly obtaining a to-be-executed task from the local queue as the first task.

[0019] In a technical solution of the above task scheduling method, the method further includes the monitoring thread obtaining the first task in the local queue in the following manner:

[0020] Continuously detect the state of the working thread multiple times. If the working thread is in the target state in all of the multiple continuous detections, then obtain the first task.

[0021] In one technical solution of the above task scheduling method, the method further includes that the monitoring thread obtains the second task in the local queue in the following manner:

[0022] For each task in the local queue, continuously detect the status variables of the task for multiple times. The status variables include a to-be-executed flag bit and an execution counter. The variable value of the to-be-executed flag bit is true or false. True indicates that the task is to be executed, and false indicates that the task has been executed. The variable value of the execution counter represents the number of times the task has been executed;

[0023] If the variable value of the to-be-executed flag bit is true in all the multiple continuous detections and the variable value of the execution counter remains unchanged, then the task is the second task.

[0024] In one technical solution of the above task scheduling method, the continuously detecting the status variables of the task for multiple times includes: continuously detecting the status variables of the task at a preset time interval for multiple times.

[0025] In one technical solution of the above task scheduling method, the variable value of the to-be-executed flag bit is set to true when the task receives task data. The continuously detecting the status variables of the task for multiple times includes:

[0026] Whenever the task receives task data once, detect the status variables of the task once until the status variables of the task are detected for multiple times.

[0027] In a second aspect, there is provided an electronic device, which includes at least one processor; and a memory communicatively connected to the at least one processor; wherein, a computer program is stored in the memory, and when the computer program is executed by the at least one processor, the method described in any one of the technical solutions of the above task scheduling method is implemented.

[0028] In a third aspect, there is provided a computer-readable storage medium, which stores multiple program codes, and the program codes are suitable for being loaded and run by a processor to execute the method described in any one of the technical solutions of the above task scheduling method.

[0029] One or more of the above technical solutions of the present application have at least one or more of the following Beneficial effects:

[0030] In a technical solution of implementing the task scheduling method provided by the present application, N tasks can be assigned to the local queues of M worker threads, where N≥1 and M≥1; M worker threads are used to schedule and execute the tasks in their respective local queues in parallel; for each worker thread, a monitoring thread is used to obtain the target tasks in the local queue of the worker thread and transfer the target tasks to a preset task springboard, and the tasks in the task springboard are scheduled and executed by the idle worker threads; for each task in the task springboard, a monitoring thread is used to obtain the number of times the task is executed, and when the number reaches a set value, the task is assigned to the local queue of the target thread, and the target thread is the worker thread with the most executed tasks; where the target tasks include the first task and / or the second task; the first task is the task to be executed when the worker thread is in the target state, and the target state is that the load of the worker thread is the highest and there is a load imbalance among the M worker threads; the second task is the task to be executed that cannot be scheduled by the worker thread.

[0031] In the above implementation, N tasks are assigned to the local queues of M worker threads, and not all tasks are set in a global queue. Each worker thread schedules (or obtains) and executes tasks from its own local queue. Since each worker thread does not need to schedule and execute tasks from a global queue, there is no need to perform thread synchronization for each worker thread, reducing the synchronization overhead between threads.

[0032] In addition, in the above implementation, a task springboard is set up, which can temporarily store the tasks in the local queues of some worker threads (i.e., the above-mentioned target tasks) in the task springboard in some special scenarios and then transfer them to the local queues of other worker threads. Specifically, when the target task is the first task, the target task is transferred to the task springboard, and the tasks in the task springboard are scheduled and executed by the idle worker threads (equivalent to the worker threads with low load). This is equivalent to transferring the tasks in the high-load worker threads to the task springboard (i.e., reducing the load of the high-load worker threads), and then the tasks are scheduled and executed by the low-load worker threads (i.e., increasing the load of the low-load worker threads), which can balance the load among the worker threads as much as possible. When the target task is the second task, the target task is transferred to the task springboard, and the tasks in the task springboard are scheduled and executed by the idle worker threads, which can make the second task be scheduled and executed as soon as possible. At the same time, it can also avoid that other tasks behind the second task are blocked for a long time and cannot be scheduled and executed due to the second task being unable to be scheduled and executed for a long time. Description of the Drawings

[0033] Referring to the accompanying drawings, the disclosure of the present application will become easier to understand. It is easy for those skilled in the art to understand that these drawings are only for illustrative purposes and are not intended to limit the protection scope of the present application. Among them:

[0034] Figure 1 is a schematic diagram of the main steps of a task scheduling method according to an embodiment of the present application;

[0035] Figure 2 is a schematic diagram of the transfer of a task between a local queue and a task springboard according to an embodiment of the present application;

[0036] Figure 3 is a schematic diagram of the main steps of determining whether there is load imbalance among M worker threads according to an embodiment of the present application;

[0037] Figure 4 is a schematic diagram of a global queue scheduling method in the prior art;

[0038] Figure 5 is a schematic diagram of a local queue scheduling method in the prior art;

[0039] Figure 6 is a schematic diagram of a task scheduling method according to an embodiment of the present application;

[0040] Figure 7 is a schematic diagram of the main structure of an electronic device according to an embodiment of the present application.

[0041] Reference numerals:

[0042] 11: Memory; 12: Processor. Detailed implementation manners

[0043] The following describes some embodiments of the present application with reference to the accompanying drawings. Those skilled in the art should understand that these embodiments are only used to explain the technical principles of the present application and are not intended to limit the protection scope of the present application.

[0044] In the description of the present application, a "processor" may include hardware, software, or a combination of both. The processor may be a central processing unit, a microprocessor, an image processor, a digital signal processor, or any other suitable processor. The processor has data and / or signal processing functions. The processor may be implemented in software, in hardware, or in a combination of both. A computer-readable storage medium includes any suitable medium that can store program code, such as a magnetic disk, a hard disk, an optical disk, a flash memory, a read-only memory, a random access memory, and the like. The term "A and / or B" represents all possible combinations of A and B, such as only A, only B, or A and B.

[0045] In each embodiment of this application, the relevant personal information of users that may be involved is strictly in accordance with the requirements of laws and regulations, following the principles of legality, legitimacy, and necessity, and for reasonable purposes based on business scenarios, processing the personal information that users actively provide or generate during the use of products / services, as well as the personal information obtained with user authorization.

[0046] The personal information of users processed in this application may vary depending on the specific product / service scenario, and it is subject to the specific scenario of the user's use of the product / service. It may involve the user's account information, device information, driving information, vehicle information, or other relevant information. This application will treat the user's personal information and its processing with a high degree of due diligence.

[0047] This application attaches great importance to the security of the user's personal information and has taken security protection measures that meet industry standards and are reasonable and feasible to protect the user's information, preventing personal information from being accessed, publicly disclosed, used, modified, damaged, or lost without authorization.

[0048] The embodiments of the task scheduling method provided in this application will be described below.

[0049] Refer to the appendix Figure 1 , Figure 1 is a schematic diagram of the main steps of the task scheduling method according to an embodiment of this application. As Figure 1 shown, the task scheduling method in the embodiment of this application mainly includes the following steps S101 to S104.

[0050] Step S101: Assign N tasks to the local queues of M worker threads, where N≥1 and M≥1.

[0051] It should be noted that the size relationship between N and M may be any one of N<M, N = M, and N>M. For each worker thread, the worker thread may be assigned at least one task or may not be assigned a task. When not assigned a task, the worker thread is in an idle state. The embodiment does not specifically limit the size relationship between N and M and the values of N and M. For example, there are 3 worker threads T1, T2, and T3, and there are 9 tasks C1 to C9. During the initialization phase, tasks C1, C2, C3, and C4 are assigned to the local queue of worker thread T1, tasks C5 and C6 are assigned to the local queue of worker thread T2, and C7, C8, and C9 are assigned to the local queue of worker thread T3.

[0052] Step S102: Use M worker threads to schedule and execute the tasks in their respective local queues in parallel. When scheduling and executing tasks, the worker thread will first schedule (or obtain) a task to be executed from the local queue and then execute the task.

[0053] Step S103: For each worker thread, a monitoring thread is used to obtain the target tasks in the local queue of the worker thread and transfer the target tasks to a preset task springboard. The tasks in the task springboard are scheduled and executed by the idle worker threads. As Figure 2 shown, task C is transferred from the local queue of the worker thread to the preset task springboard.

[0054] The preset task springboard can be understood as a queue.

[0055] An idle worker thread can be understood as a worker thread that has no task being executed and there are no tasks to be executed in the local queue of the worker thread.

[0056] The target tasks may include first tasks and / or second tasks. The second task is a to-be-executed task that cannot be scheduled by the worker thread. The first tasks are described below.

[0057] The first task is a to-be-executed task when the worker thread is in a target state. The target state is that the load of the worker thread is the highest and there is a load imbalance among M worker threads.

[0058] The load of a worker thread may be the CPU resources consumed when the CPU runs the worker thread. The more resources consumed, the higher the load, and vice versa. Based on this, the load of a worker thread can also be understood as the CPU load of the worker thread.

[0059] The highest load of a worker thread means that the load of this worker thread is the highest among the loads of all worker threads. When the load difference between at least two of the M worker threads is large, it can be determined that there is a load imbalance among the M worker threads; otherwise, there is no load imbalance.

[0060] In some embodiments, the monitoring thread can Figure 3 through the following steps S1031 to S1032 shown, determine whether there is a load imbalance among the M worker threads.

[0061] Step S1031: Detect the loads of each of the M worker threads, and obtain the first worker thread with the highest load and the second worker thread with the lowest load. Step S1032: Obtain the load difference between the first worker thread and the second worker thread; if the load difference is greater than the set threshold, there is a load imbalance among the M worker threads; otherwise, there is no load imbalance.

[0062] When setting the above set threshold, those skilled in the art can conduct task scheduling experiments to obtain the load difference between the first and second worker threads when there is a load imbalance among multiple worker threads. Multiple load differences can be obtained through multiple experiments, and the smallest one is selected. The value of the above set threshold is set according to this smallest load difference.

[0063] Based on the method described in the above step S1031 to step S1032, it is possible to quickly and accurately determine whether load imbalance occurs by using the two working threads with the highest and lowest loads.

[0064] Step S104: For each task in the task springboard, a monitoring thread is used to obtain the number of times the task is executed, and when the number reaches a set value, the task is assigned to the local queue of the target thread, where the target thread is the working thread that has executed the task the most times. As Figure 2 shown, task C is transferred out of the task springboard and reassigned to the local queue of the target thread.

[0065] The set value is a parameter with a customizable configuration value, and the set value is at least 2. In some embodiments, the above set value can be greater than the number M of working threads. In some embodiments, if there are multiple target threads, the task can be assigned to the local queue of any one of the target threads.

[0066] In some embodiments, a count map variable is set for the task in the task springboard. The count map variable is used to record the number of times different working threads schedule and execute the task from the task springboard. Whenever the task is scheduled and executed by a working thread from the task springboard, the execution count corresponding to this working thread in the count map variable is incremented by 1. By comparing the execution counts corresponding to each working thread in the count map variable, the target thread can be obtained; by summing up the execution counts corresponding to all working threads, the total number of times the task is executed can be obtained, and when the total number reaches the set value, the task is assigned to the local queue of the target thread.

[0067] Next, in conjunction with the attached Figure 4 to the attached Figure 6 , the technical effects of the task scheduling method described in the above steps S101 to S104 will be described. Figure 4 is a schematic diagram of the global queue scheduling method in the prior art, Figure 5 is a schematic diagram of the local queue scheduling method in the prior art, Figure 6 is a schematic diagram of the task scheduling method described in the above steps S101 to S104.

[0068] First, refer to the attached Figure 4 , in this example, there are 3 working threads T1, T2, and T3, and there are 9 tasks C1 to C9. All these 9 tasks are set in a global queue. When the task receives task data, the task status is to be executed. When the task is executed, the task status is executed. When the task does not receive task data, the task status is waiting for task data. Task data can be understood as the data that the task needs to process. In Figure 4In stage (a), the statuses of tasks C1, C3, C4, C7, and C8 are all to be executed, and the statuses of the remaining tasks are all waiting for task data. In Figure 4 In stage (b), after worker thread T1 obtains the lock of the global queue, it traverses the tasks in the global queue and obtains the first task to be executed (i.e., task C1). Worker threads T2 and T3 wait for the lock to be released. In Figure 4 In stage (c), after worker thread T1 schedules task C1, it releases the lock and executes task C1. After worker thread T2 obtains the lock of the global queue, it traverses the tasks in the global queue and obtains the first task to be executed (i.e., task C3). In Figure 4 In stage (d), after worker thread T2 schedules task C3, it releases the lock and executes task C3. After worker thread T3 obtains the lock of the global queue, it traverses the tasks in the global queue and obtains the first task to be executed (i.e., task C4). It can be determined from this that in the above method, only one worker thread can traverse the global queue at the same time, and other worker threads have to wait, resulting in relatively large synchronization overhead among worker threads.

[0069] Refer to the appendix Figure 5 , in this example, there are 3 worker threads T1, T2, and T3, and 9 tasks C1 to C9. Tasks C1 to C4 are bound to the local queue of worker thread T1, tasks C5 and C6 are bound to the local queue of worker thread T2, and tasks C7 to C9 are bound to the local queue of worker thread T3. In Figure 5 In stage (a), the statuses of tasks C1, C3, C4, C7, and C8 are all to be executed, and the statuses of the remaining tasks are all waiting for task data. In Figure 5 In stage (b), each worker thread schedules and executes tasks from its respective local queue in parallel. Among them, worker thread T1 schedules and executes task C1, and worker thread T3 schedules and executes task C7. Since there are no tasks to be executed in the local queue of worker thread T2, worker thread T2 does not work. In Figure 5 Load imbalance (or uneven load) occurs in stage (c). There are 4 tasks in the local queue of worker thread T1, and it frequently receives task data (i.e., frequently enters the to-be-executed state), so worker thread T1 has to frequently schedule and execute tasks; while there are 2 tasks in the local queue of worker thread T2, and these 2 tasks have not received task data all the time (i.e., have not entered the to-be-executed state for a long time), so worker thread T2 will not work all the time and is in an idle state. In Figure 5Task blocking occurs in stage (c). When worker thread T1 cannot be scheduled to task C1 for a long time or the execution time of task C1 is relatively long after being scheduled to task C1, then tasks C3 and C4 will not be scheduled and executed for a long time and will be in a blocked state. At this time, even if worker threads T2 and T3 are idle, they cannot schedule and execute tasks C3 and C4 from the local queue of worker thread T1.

[0070] See the appendix Figure 6 , in this example, there are 3 worker threads T1, T2, and T3, and 9 tasks C1 to C9. Figure 6 Stage (a) is the initialization stage. Tasks C1 to C4 are assigned to the local queue of worker thread T1, tasks C5 and C6 are assigned to the local queue of worker thread T2, tasks C7 to C9 are assigned to the local queue of worker thread T3. The states of tasks C1, C3, C4, C7, and C8 are all to be executed, and the states of the remaining tasks are all waiting for task data. In Figure 6 In stage (b), each worker thread schedules and executes tasks from its respective local queue in parallel. Among them, worker thread T1 schedules and executes task C1, and worker thread T3 schedules and executes task C7. Since there are no tasks to be executed in the local queue of worker thread T2, worker thread T2 does not work. Since all tasks are not set in a global queue, there will be no problem that only one worker thread can traverse the global queue at the same time and other worker threads have to wait, resulting in a relatively large synchronization overhead between worker threads. In Figure 6 In stage (c), task C4 in the local queue of worker thread T1 is the target task (a task to be executed that cannot be scheduled by worker thread T1, or a task to be executed when worker thread T1 is in the target state. The target state means that the load of worker thread T1 is the highest and there is a load imbalance among the 3 worker threads), and task C4 is transferred to the task jump board. In Figure 6 In stage (d), after each worker thread finishes executing the tasks in its respective local queue, it will be in an idle state. The idle worker thread will check whether there are tasks to be executed in the task jump board; if so, the idle worker thread can schedule and execute the task from the task jump board. As Figure 6 shown, worker thread T2 is an idle worker thread. It checks that task C4 to be executed is in the task jump board and will schedule and execute task C4. In Figure 6 In stage (e), when the number of times a task in the task jump board is executed reaches the set value, the task is assigned to the local queue of the target thread (the worker thread that executes the task the most times). As Figure 6As shown, the target thread is the worker thread T2. Therefore, task C4 is reassigned to the local queue of worker thread T2. When task C4 is a to-be-executed task that cannot be scheduled by worker thread T1, through the above stages (c) to (e), task C4 can be scheduled and executed as soon as possible; when task C4 is a to-be-executed task when worker thread T1 is in the target state, through the above stages (c) to (e), the task in the high-load worker thread T1 can be transferred to the task springboard to reduce the load of worker thread T1, and then the task can be scheduled and executed by the low-load worker thread T2 to increase the load of worker thread T2, so as to balance the loads between worker threads as much as possible.

[0071] In addition, Figure 6 the lock of the task springboard in Figure 4 is similar in meaning to the lock of the global queue in Figure 6 In Figure 6 , after an idle worker thread obtains the lock of the task springboard, it traverses the task springboard to obtain the first to-be-executed task, then releases the lock and executes the to-be-executed task. During this process, other idle worker threads wait for the release of the lock. In some embodiments, when there are multiple idle worker threads, these worker threads can compete for the lock of the task springboard in the order of the length of idle time from long to short. The longer the idle time, the more idle the worker thread is, and the greater the probability of it competing for the lock.

[0072] Next, the embodiments of the task scheduling method provided by the present application will be further described, specifically, the method for obtaining the first task and the second task in step S103 above will be described.

[0073] 1. Describe the method for obtaining the first task.

[0074] In some embodiments of the above step S103, after the monitoring thread detects that the worker thread is in the target state (the load of the worker thread is the highest and the load imbalance occurs among M worker threads), a to-be-executed task is randomly obtained from the local queue of this worker thread as the first task. Based on this, each to-be-executed task has a chance to be transferred to the task springboard and be scheduled and executed by an idle worker thread as soon as possible.

[0075] In some embodiments of the above step S103, the monitoring thread can continuously detect the state of the worker thread for multiple times. If the worker thread is in the target state in multiple consecutive detections, then the first task is obtained, for example, a to-be-executed task is randomly obtained as the first task. Through continuous multiple detections, the accuracy of the worker thread being in the target state can be ensured, and the occurrence of misdetection can be avoided as much as possible.

[0076] In some embodiments, the monitoring thread can continuously detect the status of the working thread at preset time intervals. For example, the time interval is T1, and the number of continuous detections is n1. The monitoring thread detects the status of the working thread every time interval T1. If the working thread is detected to be in the target state for n1 consecutive times, and there are n (n>1) pending tasks in the local queue of the working thread, then a pending task C is randomly obtained from the local queue as the first task to reduce the load of the working thread. Among them, T1, n1, and n are all parameters whose values can be custom-configured.

[0077] 2. Describe the method for obtaining the second task.

[0078] In some embodiments of the above step S103, the monitoring thread can obtain the second task from the local queue of the working thread through the following steps S1033 to S1034.

[0079] Step S1033: For each task in the local queue, continuously detect the status variables of the task. The status variables include a pending execution flag bit and an execution counter.

[0080] The pending execution flag bit is a boolean (bool) variable. The variable value of the execution flag bit is true or false. True indicates that the task is pending execution, and false indicates that the task has been executed. When the task receives task data, the task status is pending execution, and at this time the variable value will be set to true; when the task is executed, the task status is executed, and at this time the variable value will be set to false. Task data can be understood as the data that the task needs to process.

[0081] The variable value of the execution counter represents the number of times the task has been executed. Each time the task is executed, the variable value of the execution counter will be incremented by 1. In some embodiments, the execution counter is a 64-bit integer (uint64_t) variable.

[0082] Step S1034: If the variable value of the pending execution flag bit is true in consecutive multiple detections, and the variable value of the execution counter remains unchanged, then the task is the second task; otherwise, the task is not the second task. If the variable value of the pending execution flag bit is true in consecutive multiple detections, it indicates that the task is in the pending execution state in multiple detections, and the variable value of the execution counter remaining unchanged indicates that the task has not been executed in multiple detections, which means that the task cannot be scheduled by the working thread. Therefore, the task is used as the second task.

[0083] Based on the method described in the above steps S1033 to S1034, the status variables of the task can be used to conveniently and accurately detect whether the task cannot be scheduled by the working thread.

[0084] Next, the method for obtaining the second task will be further described, specifically, step S1033 above will be described.

[0085] In some embodiments of the above step S1033, the monitoring thread can continuously detect the status variable of the task at a preset time interval.

[0086] For example, the time interval is T2, and the number of continuous detections is n2. The monitoring thread detects the status variable of the task every time interval T2. If the variable value of the execution flag bit is detected as true for n2 consecutive times, and the variable value of the execution counter is equal in n2 consecutive detections, then it indicates that the task cannot be scheduled by the corresponding working thread. The task is taken as the second task and transferred to the task springboard. Here, both T2 and n2 are parameters whose values can be customized and configured.

[0087] In some embodiments of the above step S1033, the variable value of the to-be-executed flag bit is set to true when the task receives task data. The monitoring thread can detect the status variable of the task every time the task receives task data until the status variable of the task is detected multiple times. For example, the number of continuous detections is n3. Every time the task receives task data, the monitoring thread detects the status variable of the task. If the variable value of the execution flag bit is detected as true for n3 consecutive times, and the variable value of the execution counter is equal in n3 consecutive detections, then it indicates that the task cannot be scheduled by the corresponding working thread. The task is taken as the second task. Here, n3 is a parameter whose value can be customized and configured.

[0088] Next, an embodiment of the task scheduling method provided in this application will be further described, specifically, an application scenario of this application will be described.

[0089] In an application scenario provided in this application, the task scheduling method provided in this application is executed on the Advanced Driving Assistance System of an intelligent device. The intelligent device can include devices such as a driving device, an intelligent vehicle, and a robot. The Advanced Driving Assistance System will generate a large amount of data, thus giving rise to a large number of task requirements. These tasks can be understood as data-driven high-concurrency tasks. If the global queue scheduling method and the local queue scheduling method in the prior art are adopted, it will affect the stable and reliable operation of the Advanced Driving Assistance System. However, by adopting the task scheduling method provided in this application in the Advanced Driving Assistance System, it can effectively reduce the synchronization overhead between different working threads, reduce the task blocking of threads, and achieve load balancing of threads, thereby effectively improving the stability and reliability of the operation of the Advanced Driving Assistance System.

[0090] Taking the data recording application in an advanced driver assistance system in a Hardware-In-the-Loop (HIL) simulation environment as an example, compared with not adopting the task scheduling method provided by this application, when the data recording application runs in two different modes respectively under the condition of adopting the task scheduling method provided by this application, the occupancy rates of the data recording application on the CPU decrease by 14.2% and 19.7% respectively.

[0091] In addition, after HIL simulation and actual tests, it can also be determined that the task scheduling method provided by this application has a high anti-interference ability, and applications such as data recording and data mining in the advanced driver assistance system that are simulated or tested all have high stability.

[0092] It should be noted that although the above embodiments describe the various steps in a specific order, those skilled in the art can understand that in order to achieve the effects of this application, different steps do not necessarily have to be executed in such an order. They can be executed simultaneously (in parallel) or in other orders, and these adjusted solutions are equivalent technical solutions to the technical solutions described in this application, and thus will also fall within the protection scope of this application.

[0093] Those skilled in the art can understand that all or part of the processes in the method for implementing the above embodiment of this application can also be completed by instructing relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by a processor, the steps of the above various method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in the form of source code, object code, executable file or some intermediate form, etc. The computer-readable storage medium can include: any entity or device, medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory, random access memory, electrical carrier signal, telecommunication signal, and software distribution medium, etc., that can carry the computer program code.

[0094] On the other hand, this application also provides a computer-readable storage medium.

[0095] In an embodiment of a computer-readable storage medium according to the present application, the computer-readable storage medium may be configured to store a program for executing the task scheduling method in the above method embodiment. The program may be loaded and run by a processor to implement the above task scheduling method. For the sake of convenience of description, only the parts related to the embodiments of the present application are shown. For the specific technical details not disclosed, please refer to the method part of the embodiments of the present application. The computer-readable storage medium may be a storage device formed by various electronic devices. Optionally, the computer-readable storage medium in the embodiments of the present application is a non-transitory computer-readable storage medium.

[0096] Another aspect of the present application also provides an electronic device.

[0097] In an embodiment of an electronic device according to the present application, the electronic device may include at least one processor; and a memory communicatively connected to the at least one processor; wherein, a computer program is stored in the memory, and when the computer program is executed by the at least one processor, the method described in any of the above embodiments is implemented. Refer to the attached Figure 7 , Figure 7 It is exemplarily shown in the figure that the memory 11 and the processor 12 are communicatively connected through a bus.

[0098] The electronic device described in the present application may be, but is not limited to, a mobile phone, a tablet computer, a desktop computer, a laptop computer, a handheld computer, a notebook computer, a vehicle-mounted device, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), an augmented reality (AR) / virtual reality (VR) device, etc. The embodiments of the present application do not make any limitations thereto.

[0099] So far, the technical solution of the present application has been described in conjunction with an embodiment shown in the drawings. However, those skilled in the art can easily understand that the protection scope of the present application is obviously not limited to these specific embodiments. Without departing from the principle of the present application, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after these changes or substitutions will fall within the protection scope of the present application.

Claims

1. A task scheduling method, characterized in that: The method comprises: Assign N tasks to the local queues of M worker threads, N ≥ 1, M ≥ 1; Using the M worker threads to schedule and execute tasks in respective local queues in parallel; For each of the working threads, a monitoring thread is used to obtain a target task in a local queue of the working thread, and the target task is transferred to a preset task springboard, where the tasks in the task springboard are scheduled and executed by an idle working thread; For each task in the task springboard, the monitoring thread is used to obtain the number of times the task is executed, and when the number reaches a set value, the task is assigned to the local queue of the target thread, and the target thread is the working thread that executes the task the most times; in, The target task includes a first task and / or a second task; The first task is a task to be executed when the worker thread is in a target state, and the target state is that the load of the worker thread is the highest and the M worker threads are unbalanced in load; The second task is a task to be executed that cannot be scheduled by the working thread.

2. The method according to claim 1, characterized in that The method also includes the monitoring thread determining that a load imbalance occurs in the M working threads by: Detecting the load of each work thread among the M work threads, and obtaining a first work thread with the highest load and a second work thread with the lowest load; Obtain a load difference between the first working thread and the second working thread; if the load difference is greater than a set threshold, load imbalance occurs among the M working threads.

3. The method according to claim 1 or 2, characterized in that: The load of the working thread is the resources of the CPU consumed when the working thread is executed by the CPU.

4. The method according to claim 1, characterized in that: The method also includes the monitoring thread obtaining the first task in the local queue by: A to-be-executed task is randomly acquired from the local queue as the first task.

5. The method according to claim 1 or 4, characterized in that: The method also includes the monitoring thread obtaining the first task in the local queue by: The state of the working thread is detected multiple times in succession, and if the working thread is in the target state in the multiple times of detection, the first task is acquired.

6. The method according to claim 1, characterized in that The method also includes the monitoring thread obtaining the second task in the local queue by: For each task in the local queue, the state variable of the task is detected multiple times continuously, the state variable includes a pending execution flag and an execution counter, the variable value of the pending execution flag is true or false, true indicates that the task is pending execution, false indicates that the task has been executed, and the variable value of the execution counter indicates the number of times the task has been executed; If the variable value of the to-be-executed flag is true in the multiple consecutive detections, and the variable value of the execution counter remains unchanged, then the task is the second task.

7. The method according to claim 6, characterized in that The continuously detecting the state variable of the task for multiple times includes: The state variable of the task is detected multiple times continuously at preset time intervals.

8. The method according to claim 6, characterized in that The variable value of the pending execution flag is set to true when the task receives task data, and the state variable of the task is detected multiple times continuously, including: Each time the task receives task data, the state variable of the task is detected once, until the state variable of the task is detected multiple times.

9. An electronic device, characterized in that: include: at least one processor; and, a memory communicatively coupled to the at least one processor; The memory stores a computer program, and when the computer program is executed by the at least one processor, the task scheduling method according to any one of claims 1 to 8 is implemented.

10. A computer-readable storage medium storing a plurality of program codes, characterized in that: The program code is suitable for being loaded and run by a processor to execute the task scheduling method according to any one of claims 1 to 8.