Task processing method and device, equipment, storage medium and computer program product

By calling the parallel processing interface function in the main thread to create a thread pool object, the task is automatically converted to multi-threaded parallel execution, solving the complexity problem of transforming a single thread into multiple threads, improving execution efficiency and reducing development costs.

CN120704769APending Publication Date: 2025-09-26TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410358415.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-03-26
Publication Date
2025-09-26

AI Technical Summary

Technical Problem

Converting single-threaded execution code for large tasks into multi-threaded execution code is complex and incurs significant development costs. It also requires developers to manually design data splitting and merging, resulting in reduced code readability and task execution errors.

Method used

The iterative task is obtained through the main thread, and the parallel processing interface function is called to create a thread pool object, including a task scheduler and multiple worker threads. The slice data is traversed in sequence to generate work tasks, which are executed in parallel by multiple worker threads, and the results are sent back to the main thread.

Benefits of technology

It realizes the automatic conversion of tasks into multi-threaded parallel execution, improves execution efficiency, saves developer labor costs, and ensures code readability and task correctness.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120704769A_ABST
    Figure CN120704769A_ABST
Patent Text Reader

Abstract

The invention relates to a task processing method and device, computer equipment, a storage medium and a computer program product. The method comprises the steps of obtaining an iteration task through a main thread, wherein the iteration task comprises slice data and a processing function for each element in the slice data; a parallel processing interface function is called through the main thread, a thread pool object is created through the parallel processing interface function, and the thread pool object comprises a task scheduler and a plurality of working threads; elements in the slice data are traversed in sequence through a task scheduler, corresponding work tasks are generated according to processing functions for the traversed elements, and a plurality of work tasks are obtained; through the task scheduler, the multiple work tasks are sent to the multiple work threads, the multiple work threads execute the multiple work tasks in parallel to obtain the execution result, the execution result is sent back to the main thread, and the tasks can be automatically converted into multi-thread parallel execution so as to improve the execution efficiency; and multi-thread parallel computing can be realized by calling a parallel processing interface function.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a task processing method, apparatus, computer equipment, storage medium, and computer program product. Background Art

[0002] With the development of modern computer hardware, more and more computers are equipped with multi-core computing capabilities, which allows them to run multiple tasks in parallel to improve computing performance, reduce execution time, and increase efficiency. Various programming languages ​​provide interfaces related to parallel computing, allowing developers to customize the splitting scheme of large tasks, splitting a large task into multiple smaller tasks and running them in parallel on different processor cores.

[0003] However, converting single-threaded execution code for large tasks into multi-threaded execution code is a very complicated task. For different tasks, not only does it require writing a lot of code, which consumes a lot of development costs, but it also requires developers to spend time designing the reasonable segmentation of data, the reasonable splitting of each thread task, and the reasonable merging of multi-threaded execution results. This process also requires developers to consider the issues of data competition and data merging, which reduces the readability of the code and makes the task prone to execution errors. Summary of the Invention

[0004] Based on this, it is necessary to provide a task processing method, device, computer equipment, computer-readable storage medium and computer program product to address the above technical problems, which can automatically convert tasks into multi-threaded parallel execution to improve execution efficiency. Developers do not need to manually write a large amount of additional multi-threaded execution code. They only need to call the parallel processing interface function to implement multi-threaded parallel computing capabilities at the bottom layer.

[0005] In a first aspect, the present application provides a task processing method. The method comprises:

[0006] Obtaining an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data;

[0007] Calling a parallel processing interface function through the main thread, creating a thread pool object through the parallel processing interface function, wherein the thread pool object includes a task scheduler and multiple worker threads;

[0008] By means of the task scheduler, the elements in the slice data are sequentially traversed, and corresponding work tasks are generated according to the processing functions for the traversed elements, thereby obtaining a plurality of work tasks;

[0009] The task scheduler sends the multiple work tasks to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, which are then sent back to the main thread.

[0010] In a second aspect, the present application further provides a task processing device. The device comprises:

[0011] An iterative task acquisition module, configured to acquire an iterative task through a main thread, wherein the iterative task includes slice data and a processing function for each element in the slice data;

[0012] A parallel processing calling module is used to call a parallel processing interface function through the main thread, and create a thread pool object through the parallel processing interface function, wherein the thread pool object includes a task scheduler and multiple worker threads;

[0013] A task division module is used to traverse the elements in the slice data in sequence through the task scheduler, and generate corresponding work tasks according to the processing functions for the traversed elements to obtain multiple work tasks;

[0014] The task parallel module is used to send the multiple work tasks to the multiple work threads through the task scheduler, and the multiple work threads execute the multiple work tasks in parallel, obtain execution results, and send the execution results back to the main thread.

[0015] In one embodiment, the task division module is used to determine the data processing capacity of each working thread through the task scheduler based on the data length of the slice data and the number of threads of the multiple working threads; traverse the elements in the slice data in sequence, and divide the elements in the slice data into multiple data blocks according to the data processing capacity; generate corresponding work tasks according to the processing function of the elements contained in each data block, and obtain multiple work tasks.

[0016] In one embodiment, the multiple work threads are used to process work tasks of different priority levels respectively, and the task parallel module is used to obtain the task priority corresponding to each work task; and send the corresponding work task to the work thread used to process the work task of the corresponding priority level according to the task priority.

[0017] In one embodiment, the task processing device further includes:

[0018] The channel creation module is used to create a channel, where the channel is used for the multiple worker threads to send execution results to the main thread; and the execution results of each of the worker threads are collected through the main thread.

[0019] In one embodiment, the apparatus further comprises:

[0020] A queue creation module is used to create a task double-ended queue for each of the multiple working threads, wherein the task double-ended queue includes a task extraction end and a task stealing end;

[0021] The task parallel module is used to add the multiple work tasks to the task double-ended queues corresponding to the multiple work threads; through the work threads, take out the work tasks from the task take-out end of the task double-ended queue corresponding to the work threads, and execute the taken-out work tasks.

[0022] In one embodiment, the task parallel module is used to traverse the task stealing end of the task double-ended queue corresponding to each of the working threads through the task scheduler. When a working task exists at the traversed task stealing end, the working task is taken out and assigned to the working thread that is currently in an idle state.

[0023] In one embodiment, the task parallel module is further used to confirm that the working thread is currently in an idle state when the task double-ended queue corresponding to the working thread is empty; and to confirm that the working thread is currently in a non-idle state when the task double-ended queue corresponding to the working thread is not empty.

[0024] In one embodiment, the parallel processing calling module is used to obtain the number of processor cores through the parallel processing interface function, determine the number of threads based on the number of processor cores, and the number of threads is less than the number of processor cores; and create multiple working threads according to the number of threads through the parallel processing interface function.

[0025] In one embodiment, the iterative task acquisition module is used to obtain the original data set and the processing function for each element in the original data set through the main thread; convert the original data set into slice data of atomic reference counting type, and the slice data and the processing function constitute the iterative task.

[0026] In a third aspect, the present application further provides a computer device. The computer device includes a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are performed:

[0027] Obtaining an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data;

[0028] Calling a parallel processing interface function through the main thread, creating a thread pool object through the parallel processing interface function, wherein the thread pool object includes a task scheduler and multiple worker threads;

[0029] By means of the task scheduler, the elements in the slice data are sequentially traversed, and corresponding work tasks are generated according to the processing functions for the traversed elements, thereby obtaining a plurality of work tasks;

[0030] The task scheduler sends the multiple work tasks to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, which are then sent back to the main thread.

[0031] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the following steps:

[0032] Obtaining an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data;

[0033] Calling a parallel processing interface function through the main thread, creating a thread pool object through the parallel processing interface function, wherein the thread pool object includes a task scheduler and multiple worker threads;

[0034] By means of the task scheduler, the elements in the slice data are sequentially traversed, and corresponding work tasks are generated according to the processing functions for the traversed elements, thereby obtaining a plurality of work tasks;

[0035] The task scheduler sends the multiple work tasks to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, which are then sent back to the main thread.

[0036] In a fifth aspect, the present application further provides a computer program product. The computer program product includes a computer program that, when executed by a processor, implements the following steps:

[0037] Obtaining an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data;

[0038] Calling a parallel processing interface function through the main thread, creating a thread pool object through the parallel processing interface function, wherein the thread pool object includes a task scheduler and multiple worker threads;

[0039] By means of the task scheduler, the elements in the slice data are sequentially traversed, and corresponding work tasks are generated according to the processing functions for the traversed elements, thereby obtaining a plurality of work tasks;

[0040] The task scheduler sends the multiple work tasks to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, which are then sent back to the main thread.

[0041] The above-mentioned task processing method, device, computer equipment, storage medium and computer program product, the iterative task is a task originally executed by a single thread, the iterative task includes slice data and a processing function for each element in the slice data, after the iterative task is obtained by the main thread, the parallel processing interface function is called, and a thread pool object is created through the parallel processing interface function, the thread pool object includes a task scheduler and multiple worker threads, through the task scheduler, the elements in the slice data are traversed in sequence, and corresponding work tasks are generated according to the processing function for the traversed elements, and multiple work tasks are obtained, through the task scheduler, the multiple work tasks are sent to the multiple worker threads, and the multiple worker threads execute the multiple work tasks in parallel to obtain the execution results, and the execution results are sent back to the main thread. In this way, the task is automatically converted into multi-threaded parallel execution to improve the execution efficiency, and the developer does not need to manually write a large amount of additional multi-threaded execution code. It only needs to call the parallel processing interface function to realize the multi-threaded parallel computing capability at the bottom layer, saving the developer's manpower development cost, and the readability of the code can be guaranteed, and the task will not be executed incorrectly. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] Figure 1 An application environment diagram of a task processing method in one embodiment;

[0043] Figure 2 is a schematic diagram of an iterative task in one embodiment;

[0044] Figure 3 This is a schematic diagram of the code after the single-threaded code is transformed;

[0045] Figure 4 A schematic diagram of code for calling a parallel processing interface function to implement multi-threaded execution in one embodiment;

[0046] Figure 5 1 is a flowchart of a task processing method in one embodiment;

[0047] Figure 6 A flowchart illustrating steps for dividing multiple work tasks in one embodiment;

[0048] Figure 7 A schematic diagram of multi-threaded execution of multiple work tasks in one embodiment;

[0049] Figure 8 is a flowchart of a task processing method in a specific embodiment;

[0050] Figure 9 is a structural block diagram of a task processing device in one embodiment;

[0051] Figure 10 is a diagram of the internal structure of a computer device in one embodiment;

[0052] Figure 11 FIG. 4 is a diagram showing the internal structure of a computer device in another embodiment. DETAILED DESCRIPTION

[0053] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.

[0054] The task processing method provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store data that the server 104 needs to process. The data storage system can be integrated on the server 104, or it can be placed on the cloud or other servers. In one embodiment, the terminal 102 can send an iterative task to the server 104, and the server 104 obtains the iterative task through the main thread. The iterative task includes sliced ​​data and a processing function for each element in the sliced ​​data; the parallel processing interface function is called through the main thread, and a thread pool object is created through the parallel processing interface function. The thread pool object includes a task scheduler and multiple worker threads; through the task scheduler, the elements in the sliced ​​data are traversed in turn, and corresponding work tasks are generated according to the processing function for the traversed elements to obtain multiple work tasks; through the task scheduler, the multiple work tasks are sent to the multiple worker threads, and the multiple worker threads execute the multiple work tasks in parallel to obtain execution results, and the execution results are sent back to the main thread.

[0055] In a specific application scenario, terminal 102 obtains an image to be encoded and requests server 104 to encode the image. After receiving the image encoding request sent by the terminal, server 104 can regard the image encoding request as an iterative task, because image encoding sequentially encodes multiple coding units in the image. Through the task processing method provided in the embodiment of the present application, the iterative task originally executed in a single thread is automatically converted to multi-threaded parallel execution to improve execution efficiency. Moreover, there is no need for developers to manually write a large amount of additional multi-threaded execution code. Only the parallel processing interface function needs to be called to implement multi-threaded parallel computing capabilities at the bottom layer. In some embodiments, the above-mentioned task processing method can also be executed by terminal 102.

[0056] Terminal 102 may include, but is not limited to, various desktop computers, laptops, smartphones, tablet computers, IoT devices, and portable wearable devices. IoT devices may include smart speakers, smart TVs, smart air conditioners, and smart car devices. Portable wearable devices may include smart watches, smart bracelets, and head-mounted devices. Server 104 may be implemented as a standalone server or a server cluster consisting of multiple servers.

[0057] In other application scenarios, the iterative task can also be any other single-threaded task, for example, the task of sequentially calculating the absolute value of each pixel in an image, the task of sequentially calculating the mean of the neighborhood pixels of each pixel in an image, and so on.

[0058] like Figure 2 FIGURE 1 is a schematic diagram of an iterative task in one embodiment. Figure 2 The code shown is an iterative task executed by a single thread. It is used to calculate the square of each element in an integer array in turn and store the calculation results in another array. For this iterative task, if the developer needs to transform this iterative task into a parallel computing task, then when transforming the code, the developer needs to first determine how many data blocks the integer array is divided into, such as 2 data blocks, and then move each data block to each worker thread to implement parallel computing. The square of each element included in the current data block is calculated in the worker thread, and finally the execution results of each worker thread are collected. After all worker threads complete the calculation, the execution results of all worker threads are collected. The transformed code is as follows: Figure 3 shown.

[0059] As can be seen, this process requires developers to manually divide the data set into several data blocks based on its actual conditions. They also need to create multiple threads for parallel computing, allocate data blocks to each thread, and devise a reasonable method to merge the results of each thread after all threads have completed execution. This transformation process is very time-consuming for developers. Moreover, if such design and consideration are required for a large number of different iterative tasks, it will obviously be very time-consuming and costly to develop. It may also lead to inefficient code transformation, poor code readability, and code vulnerabilities.

[0060] The task processing method provided in the embodiment of the present application only needs to implement a parallel processing interface function, which receives an iterative task and finally returns a parallel iterator to implement multi-threaded parallel execution at the bottom layer. Figure 4The following is a code diagram of calling a parallel processing interface function to implement multi-threaded execution in one embodiment. In order to implement the above-mentioned parallel processing interface function, in the embodiment of this application, the following functions are designed for the parallel processing interface function based on Rust:

[0061] Rust is a systems programming language, a multi-paradigm language that supports functional, imperative, and generic programming paradigms. Rust strives to be a programming language that elegantly solves high-concurrency and high-safety system problems. It is suitable for large-scale scenarios, creating and maintaining boundaries that preserve the integrity of large systems. This leads to its emphasis on safety, memory layout control, and concurrency.

[0062] Thread pool management: Create a thread pool to manage worker threads, allowing for thread reuse instead of creating new threads for each task. The thread pool must be able to efficiently schedule tasks to threads for execution. A thread pool is a mechanism for managing the execution of parallel tasks. It creates a fixed number of threads in a program and assigns tasks to these threads for execution. In Rust, thread pools can help effectively utilize the performance of multi-core processors and achieve parallel and concurrency optimizations.

[0063] Task allocation and scheduling: Design a task scheduling system that can break down large tasks into smaller ones and assign these tasks to threads in a thread pool. This system needs to be able to balance the load to avoid starving some threads and overloading others.

[0064] Work-stealing: A thread steals tasks from other queues to execute. This allows idle threads to steal tasks from busy threads, helping to improve thread utilization and overall performance. A large task is split into several independent subtasks. To reduce contention between threads, these subtasks are placed in different queues, and a separate thread is created for each queue to execute the tasks within. There is a one-to-one correspondence between threads and queues. For example, thread 1 processes tasks in queue 1, while thread 2 processes tasks in queue 2. However, some threads may complete tasks in their own queues first, while other threads still have tasks waiting in their corresponding queues. An idle thread can steal a task from another thread's queue to execute. Multiple threads may access the same queue. Therefore, to reduce contention between the stealing thread and the task-stealing thread, a double-ended queue (DQ) is often used. The task-stealing thread retrieves tasks from the head of the DQ, while the task-stealing thread retrieves tasks from the tail.

[0065] Data segmentation: Develop a mechanism to split a data set into small pieces that can be processed independently. This usually requires data structures that support segmentation operations, such as slicing. In Rust, a slice is a data structure that describes a group of data of the same type, of uncertain length, and stored contiguously in memory, expressed as [T]. A slice is a view of the collection data. Through the iter() method of the slice, an iterator can be generated to iterate over the slice. In Rust, iterators are an abstract way to provide serialized access to elements. Through iterators, a data stream can be easily traversed and variously processed, including mapping, filtering, and evaluation. The iterator in Rust gets the next element through the next() method. If next() returns None, it means that all elements in the iterator have been taken out.

[0066] Synchronization and concurrency control: Ensure correctness when concurrently accessing shared resources. Mutex locks, atomic operations, or other synchronization mechanisms can be used.

[0067] Parallel computing: Parallelism refers to the ability to execute multiple tasks simultaneously on different processor cores. These tasks can be executed in parallel, with each task running independently on its own processor core. Parallelism is often used to accelerate computationally intensive tasks by utilizing multiple processor cores to execute multiple tasks simultaneously, thereby increasing overall computing speed.

[0068] All tasks of a process are executed within threads. For a process to execute tasks, it must have threads, and a process must have at least one thread. A thread is the basic execution unit of a process, responsible for executing tasks within the process. Multiple threads can run concurrently within a process, each executing different tasks in parallel.

[0069] Data contention refers to the situation in multi-threaded programming where two or more threads access shared data at the same time, and at least one thread writes to the shared data, causing the program to exhibit undefined behavior.

[0070] Resource contention: Multiple threads access the same shared resource at the same time, such as the same variable or the same file, resulting in resource contention, which affects the correctness and performance of the program.

[0071] Memory access conflict: Multiple threads access the same memory address at the same time, resulting in memory access conflict, which can cause the program to exhibit undefined behavior, such as crashes and deadlocks.

[0072] Race condition: Multiple threads execute the same code block at the same time, resulting in an uncertain order of execution of the code, which may cause the program to exhibit undefined behavior.

[0073] In one embodiment, Figure 5 As shown, a task processing method is provided, which is applied to Figure 1 The computer device (terminal 102 or server 104) in FIG. 1 is used as an example to illustrate the method, which includes the following steps:

[0074] Step 502: Obtain an iterative task through the main thread, where the iterative task includes slice data and a processing function for each element in the slice data.

[0075] The main thread is the thread that executes the main method. Starting from the main method, the main thread executes sequentially from top to bottom. During execution, the main thread can call parallel processing interface functions to create multiple worker threads and a task scheduler. The main method is the main method. The computing device obtains iterative tasks through the main thread. Iterative tasks consist of sliced ​​data and a processing function for each element in the sliced ​​data.

[0076] An iterative task is a task that is executed serially by a single thread, and the task includes slice data and a processing function for each element in the slice data. The single-threaded iterative task traverses each element of the slice data in turn, and applies the corresponding processing function to each traversed element to obtain a processing result for each element. A slice is a data structure that supports splitting operations, and slice data is slice-type data. In an embodiment of the present application, when the iterative task is received, the slice data and the processing function for each element in the slice data are taken out from the iterative task. The processing functions for different elements can be the same or different. The embodiment of the present application is mainly explained by taking the same processing function as an example. In order to realize the conversion of the iterative task into multiple work tasks executed by multiple threads, the computer device will next call the parallel processing interface function to perform the conversion based on the slice data and the processing function.

[0077] It should be noted that the computer device can first obtain the original data set and the processing function for each element in the original data set through the main thread, and then convert the original data set into slice data of atomic reference counting type. The slice data and the processing function constitute the iterative task.

[0078] Step 504: Call the parallel processing interface function through the main thread, and create a thread pool object through the parallel processing interface function. The thread pool object includes a task scheduler and multiple worker threads.

[0079] Among them, the thread pool object is a custom structure, namely the thread pool structure. The thread pool object includes a task scheduler and multiple worker threads. In an embodiment of the present application, by declaring a thread pool structure to manage all worker threads, the worker threads can be reused, instead of creating a new thread every time a single-threaded iterative task needs to be converted to multi-threaded parallel execution, so that work tasks can be efficiently scheduled to threads for execution. The task scheduler can be used to send tasks and add tasks to the thread pool through some methods. In an embodiment of the present application, by declaring a worker thread structure, the worker thread structure can wait for and execute the received tasks in an infinite loop, and the worker thread can receive the tasks sent by the task scheduler through some methods. The method for adding tasks to the thread pool in the thread pool and the method for receiving tasks in the worker thread can communicate through a channel.

[0080] For example, the thread pool structure is defined as follows:

[0081] Type Job = Box<dyn Fn0nce()+ Send +‘static’> ;

[0082] struct ThreadPool{

[0083] workers:Vec <worker>,

[0084] sender:mpsc::Sender <job>,

[0085] }

[0086] Among them, the Job type is a task, which is a closure that can be sent to the thread pool. The closure contains the code that needs to be run in the newly created worker thread, which is the work task.

[0087] For example, the defined worker thread structure is as follows:

[0088] struct Worker{

[0089] id: usize,

[0090] thread:Option <thread :: JoinHandle<()> >,

[0091] }

[0092] After receiving the above-mentioned slice data and processing function, the computer device can call the parallel processing interface function and create a new thread pool of the above-mentioned structure type through the parallel processing interface function. Specifically, through the parallel processing interface function, the number of processor cores is obtained, and the number of threads is determined based on the number of processor cores, which is less than the number of processor cores; through the parallel processing interface function, multiple worker threads are created according to the number of threads. For example, if the computer device includes 4 processor cores, the new thread pool created by default can include 4 worker threads, and the worker threads are initialized. The worker threads in these thread pools can be used to receive work tasks.

[0093] Step 506: Through the task scheduler, the elements in the slice data are traversed in sequence, and corresponding work tasks are generated according to the processing functions for the traversed elements to obtain multiple work tasks.

[0094] Among them, the task scheduler is used to break down large tasks into small tasks and assign these tasks to threads in the thread pool. To implement a task scheduler, you can add some logic to the thread pool structure to manage the assignment of tasks. First, define the task type of the work task, such as a closure. The work task can accept a data block of sliced ​​data and perform some work. By modifying the thread pool structure, a new method is added to the structure to accept data slices and break them down into smaller data blocks for parallel calculation. For example, a schedule function is added to the thread pool structure to traverse the elements in the slice data in sequence. After dividing the slice data into multiple data blocks, a corresponding work task is created for each data block to obtain multiple work tasks.

[0095] In one embodiment, Figure 6 As shown, the task scheduler sequentially traverses the elements in the slice data and generates corresponding work tasks according to the processing functions for the traversed elements, thereby obtaining multiple work tasks, including:

[0096] Step 602: Determine the data processing capacity of each working thread according to the data length of the slice data and the number of the multiple working threads through the task scheduler.

[0097] Specifically, the task scheduler first converts the slice data into a data type that can be safely shared across threads to avoid data concurrency contention issues. Threads can correctly process shared data and resources without causing data inconsistencies or other errors. The task scheduler then calculates the size of the data block that each worker thread should process, i.e., the data processing capacity. This can be determined by dividing the data length of the slice data by the number of worker threads to ensure that each worker thread can be scheduled for at least one work task. For example, if the number of worker threads is 4 and the data length of the slice data is 5, each worker thread can execute at least one work task.

[0098] Step 604: traverse the elements in the slice data in sequence, and divide the elements in the slice data into multiple data blocks according to the data processing amount.

[0099] After determining the task processing capacity of each worker thread, the task scheduler can divide the elements in the slice data into multiple data blocks according to the data processing capacity. Figure 2 In the example shown, the slice data is an integer array [1, 2, 3, 4, 5] with a data length of 5. By traversing the elements in the slice data, the integer data is divided into 4 data blocks, where 1 data block includes 2 elements and the other data blocks each include 1 element.

[0100] Step 606: Generate corresponding work tasks according to the processing function for the elements included in each data block to obtain multiple work tasks.

[0101] For each divided data block, a closure-type work task is created for each data block based on the element-specific processing function, resulting in multiple work tasks. For example, in the above example, the integer array is divided into four data blocks, namely 1, 2, 3, 4, and 5. These four data blocks are traversed and corresponding work tasks A, B, C, D, and E are generated for each. The processing function is used to square elements. Therefore, the work task A generated by the processing function is the task of squaring element "1" in the first data block, work task B is the task of squaring element "2" in the second data block, work task C is the task of squaring element "3" in the third data block, work task D is the task of squaring element "4" in the fourth data block, and work task E is the task of squaring element "5" in the fourth data block.

[0102] In this embodiment, the automatic task scheduling mechanism can dynamically divide the slice data into multiple data blocks according to the data length of the slice data and the number of available processor cores (i.e., the number of working threads), generate corresponding work tasks for each data block, and assign the work tasks to different working threads. This can fully utilize computing resources and improve the efficiency of parallel computing.

[0103] Step 508: Send the multiple work tasks to the multiple work threads through the task scheduler, and have the multiple work threads execute the multiple work tasks in parallel to obtain execution results, and send the execution results back to the main thread.

[0104] In the above example, task A can be sent to worker thread 1, task B can be sent to worker thread 2, task C can be sent to worker thread 3, and task D and task E can be sent to worker thread 4. The four worker threads execute the assigned tasks in parallel, which can improve the overall efficiency of calculating the entire data set.

[0105] like Figure 7 FIG2 is a schematic diagram of a multi-threaded execution of multiple work tasks in one embodiment. Figure 7 , according to the number of processor cores of the computer device, multiple worker threads are created to convert the data set into a data structure that can support segmentation, that is, sliced ​​data. Then the task scheduler divides the sliced ​​data according to the data length and the worker threads to obtain multiple data blocks. For each element in each data block, a corresponding work task is generated according to the processing function for each element in the data set, and these work tasks are sent to multiple worker threads to ensure that each worker thread is assigned a work task for parallel execution.

[0106] In the above-mentioned task processing method, the iterative task is a task originally executed by a single thread. The iterative task includes sliced ​​data and a processing function for each element in the sliced ​​data. After the iterative task is obtained by the main thread, the parallel processing interface function is called, and a thread pool object is created through the parallel processing interface function. The thread pool object includes a task scheduler and multiple worker threads. Through the task scheduler, the elements in the sliced ​​data are traversed in sequence, and corresponding work tasks are generated according to the processing functions for the traversed elements to obtain multiple work tasks. Through the task scheduler, the multiple work tasks are sent to multiple worker threads, and the multiple worker threads execute the multiple work tasks in parallel to obtain the execution results, and send the execution results back to the main thread. In this way, the task is automatically converted into multi-threaded parallel execution to improve execution efficiency. Developers do not need to manually write a large amount of additional multi-threaded execution code. They only need to call the parallel processing interface function to realize multi-threaded parallel computing capabilities at the bottom layer, saving the developer's manpower development costs, and the readability of the code can be guaranteed, and the task will not be executed incorrectly.

[0107] In one embodiment, the multiple work threads are respectively used to process work tasks of different priority levels, and sending the multiple work tasks to the multiple work threads includes:

[0108] Get the task priority corresponding to each work task;

[0109] According to the task priority, the corresponding work task is sent to the work thread for processing the work task of the corresponding priority level.

[0110] In an embodiment of the present application, after the working thread completes the task at hand, it will repeatedly obtain tasks from the work task queue to execute in an infinite loop. For work tasks with higher priority, they can be scheduled to the corresponding working thread first to ensure that some higher priority tasks are executed first.

[0111] In one embodiment, the method further comprises:

[0112] Create a channel for the multiple worker threads to send execution results to the main thread;

[0113] The execution results of each worker thread are collected through the main thread.

[0114] Specifically, after creating the thread pool, the computer device may create a channel for transmitting execution results between the worker threads of the thread pool and the main thread.

[0115] In Rust, a channel is a tool for concurrent message passing. A channel consists of a sender and a receiver. One part of the code calls the sender's methods and the data to be sent, while another part of the code checks the message received by the receiver. When either the sender or the receiver is discarded, the channel is considered closed. A channel can have multiple senders that generate tasks or data, but can only have one receiver that receives tasks or data. In the embodiment of the present application, the newly created multiple worker threads need to own the sender of the channel so that they can send task execution results to the channel. The sender of the channel has a sender method to obtain the data to be placed in the channel. The receiver of the channel has a receiver method to receive data from the channel.

[0116] In one embodiment, the method further comprises:

[0117] A task double-ended queue is created for each of the multiple worker threads, where the task double-ended queue includes a task taking end and a task stealing end.

[0118] The multiple work tasks are sent to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, including:

[0119] Add the multiple work tasks to the task double-ended queue corresponding to the multiple work threads;

[0120] Through the working thread, the working task is taken out from the task taking end of the task double-ended queue corresponding to the working thread, and the taken out working task is executed.

[0121] In this implementation, idle worker threads are allowed to steal work from busy worker threads, which helps improve thread utilization and overall performance. In Rust, implementing a work-stealing algorithm typically requires using a double-ended queue (deque) of tasks, with worker threads taking tasks from one end and allowing other worker threads to steal tasks from the other end. The following is a simplified implementation of a work-stealing thread pool, modified from the previous code example:

[0122] struct ThreadPool{

[0123] workers:Vec <worker>,

[0124] sender:mpsc::Sender <job>,

[0125] stealers: Vec<mpsc::Receiver <job>>,

[0126] }

[0127] Also, add a new function steal() method in the main method to steal work tasks.

[0128] As can be seen from the code example above, the thread pool structure now contains a stealers vector, which contains the task stealing ends of all worker threads.

[0129] In one embodiment, the method further comprises:

[0130] The task scheduler traverses the task stealing end of the task double-ended queue corresponding to each working thread, and when a working task exists at the traversed task stealing end, the working task is taken out and assigned to the working thread that is currently in an idle state.

[0131] Specifically, the steal() method stated above can be used to traverse all task stealing ends and try to steal work tasks from them. If the work task is successfully stolen, it will send the work task back to the sending end of the thread pool so that any idle thread can receive and execute the stolen work task.

[0132] In one embodiment, an idle flag can be declared in the worker thread. Modify the worker thread structure as follows:

[0133] struct Worker{

[0134] id: usize,

[0135] thread:Option <thread :: JoinHandle<()> >,

[0136] busy:Arc <atomicbool>,

[0137] }

[0138] Now each worker thread has an additional busy flag, which is an AtomicBool, an atomic Boolean type used to implement atomic operations. This flag indicates whether the worker thread is executing a task, that is, whether it is idle.

[0139] Based on the thread pool implemented above, the final parallel processing interface function can be implemented, which can perform parallel operations on the data set and provide methods to collect the execution results.

[0140] In a specific embodiment, Figure 8 As shown, the above task processing method includes the following steps:

[0141] Step 802: Obtain an iterative task through the main thread, where the iterative task includes slice data and a processing function for each element in the slice data;

[0142] Step 804: Calling a parallel processing interface function through the main thread, and creating a thread pool object through the parallel processing interface function. The thread pool object includes a task scheduler and multiple worker threads.

[0143] Step 806: Determine, by the task scheduler, the data processing capacity of each working thread according to the data length of the slice data and the number of threads of the plurality of working threads;

[0144] Step 808, traversing the elements in the slice data in sequence, and dividing the elements in the slice data into multiple data blocks according to the data processing amount;

[0145] Step 810: Generate corresponding work tasks according to the processing function for the elements contained in each data block, and obtain multiple work tasks;

[0146] Step 812: Create a task double-ended queue for each of the multiple worker threads, where the task double-ended queue includes a task take-out end and a task stealing end.

[0147] Step 814: Add the plurality of work tasks to the task double-ended queues corresponding to the plurality of work threads through the task scheduler;

[0148] Step 816: The worker thread retrieves the work task from the task dequeue corresponding to the worker thread, executes the retrieved work task, obtains an execution result, and sends the execution result back to the main thread.

[0149] Step 818: traverse the task stealing end of the task double-ended queue corresponding to each worker thread through the task scheduler. When a work task exists at the traversed task stealing end, take out the work task and assign it to the worker thread that is currently in an idle state.

[0150] Step 820: Create a channel, which is used for the multiple worker threads to send execution results to the main thread;

[0151] Step 822: Collect the execution results of each worker thread through the main thread.

[0152] The task processing method provided by the embodiment of the present application only requires receiving an iterative task, then creating a thread pool, and then creating a worker task for each element in the iterative task. Each worker task will apply a processing function and send the processing result back to the main thread. Finally, the main thread collects the execution results of all workers and collects them into a data object.

[0153] Furthermore, for large tasks that can be executed in parallel under different scenarios, developers only need to call the parallel processing interface function in the main method to implement parallel computing capabilities at the bottom level without having to write a large amount of additional code required for parallel scenarios. This simple and easy-to-use approach to writing parallel code makes it safer and more reliable, and converts serial code into parallel code without the need for manual thread and lock management. The automatic task scheduling mechanism dynamically divides the original task into multiple work tasks and assigns them to different work threads based on the workload of the original task and the number of available processor cores, which can fully utilize computing resources and improve the efficiency of parallel computing.

[0154] It should be understood that, although the various steps in the flowcharts involved in the various embodiments as above are shown in sequence according to the instructions of the arrows, these steps are not necessarily performed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be performed in other orders. Moreover, at least a portion of the steps in the flowcharts involved in the various embodiments as above can include multiple steps or multiple stages, and these steps or stages are not necessarily performed at the same time, but can be performed at different times, and the execution order of these steps or stages is not necessarily performed in sequence, but can be performed in turn or alternately with other steps or at least a portion of the steps or stages in other steps.

[0155] Based on the same inventive concept, embodiments of the present application also provide a task processing device for implementing the aforementioned task processing method. The implementation solution provided by this device is similar to the implementation solution described in the aforementioned method. Therefore, the specific limitations in one or more of the following task processing device embodiments can be found in the above-mentioned limitations on the task processing method and will not be further elaborated here.

[0156] In one embodiment, Figure 9 As shown, a task processing device 900 is provided, comprising: an iterative task acquisition module 902, a parallel processing calling module 904, a task division module 906 and a task parallel module 908, wherein:

[0157] Iterative task acquisition module 902, used to acquire an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data;

[0158] A parallel processing calling module 904 is configured to call a parallel processing interface function through the main thread, and create a thread pool object through the parallel processing interface function. The thread pool object includes a task scheduler and multiple worker threads.

[0159] The task division module 906 is used to sequentially traverse the elements in the slice data through the task scheduler, and generate corresponding work tasks according to the processing functions for the traversed elements to obtain multiple work tasks;

[0160] The task parallel module 908 is used to send the multiple work tasks to the multiple work threads through the task scheduler, and the multiple work threads execute the multiple work tasks in parallel, obtain execution results, and send the execution results back to the main thread.

[0161] In one embodiment, the task division module is used to determine the data processing capacity of each working thread through the task scheduler based on the data length of the slice data and the number of threads of the multiple working threads; traverse the elements in the slice data in sequence, and divide the elements in the slice data into multiple data blocks according to the data processing capacity; generate corresponding work tasks according to the processing function of the elements contained in each data block, and obtain multiple work tasks.

[0162] In one embodiment, the multiple work threads are used to process work tasks of different priority levels respectively, and the task parallel module is used to obtain the task priority corresponding to each work task; and send the corresponding work task to the work thread used to process the work task of the corresponding priority level according to the task priority.

[0163] In one embodiment, the task processing device 900 further includes:

[0164] The channel creation module is used to create a channel, which is used for the multiple worker threads to send execution results to the main thread; and the execution results of each worker thread are collected through the main thread.

[0165] In one embodiment, the task processing device 900 further includes:

[0166] A queue creation module is used to create a task double-ended queue for each of the multiple working threads, wherein the task double-ended queue includes a task extraction end and a task stealing end;

[0167] The task parallel module is used to add the multiple work tasks to the task double-ended queues corresponding to the multiple work threads; through the work thread, the work tasks are taken out from the task extraction end of the task double-ended queue corresponding to the work thread, and the extracted work tasks are executed.

[0168] In one embodiment, the task parallel module is used to traverse the task stealing end of the task double-ended queue corresponding to each working thread through the task scheduler. When a working task exists at the traversed task stealing end, the working task is taken out and assigned to the working thread that is currently in an idle state.

[0169] In one embodiment, the task parallel module is further used to confirm that the worker thread is currently in an idle state when the task double-ended queue corresponding to the worker thread is empty; and to confirm that the worker thread is currently in a non-idle state when the task double-ended queue corresponding to the worker thread is not empty.

[0170] In one embodiment, the parallel processing calling module is used to obtain the number of processor cores through the parallel processing interface function, determine the number of threads based on the number of processor cores, and the number of threads is less than the number of processor cores; and create multiple working threads according to the number of threads through the parallel processing interface function.

[0171] In one embodiment, the iterative task acquisition module is used to obtain the original data set and the processing function for each element in the original data set through the main thread; convert the original data set into slice data of atomic reference counting type, and the slice data and the processing function constitute the iterative task.

[0172] The above-mentioned task processing device 900, the iterative task is a task originally executed by a single thread, and the iterative task includes slice data and a processing function for each element in the slice data. After the iterative task is obtained by the main thread, the parallel processing interface function is called, and a thread pool object is created through the parallel processing interface function. The thread pool object includes a task scheduler and multiple worker threads. Through the task scheduler, the elements in the slice data are traversed in sequence, and corresponding work tasks are generated according to the processing functions for the traversed elements to obtain multiple work tasks. Through the task scheduler, the multiple work tasks are sent to multiple worker threads, and the multiple worker threads execute the multiple work tasks in parallel to obtain execution results, and the execution results are sent back to the main thread. In this way, the task is automatically converted into multi-threaded parallel execution to improve execution efficiency. Developers do not need to manually write a large amount of additional multi-threaded execution code. They only need to call the parallel processing interface function to realize multi-threaded parallel computing capabilities at the bottom layer, saving the developer's human development costs, and the readability of the code can be guaranteed, and the task will not be executed incorrectly.

[0173] Each module in the task processing device 900 may be implemented in whole or in part by software, hardware, or a combination thereof. Each module may be embedded in or independent of a processor in a computer device in the form of hardware, or may be stored in a memory in the computer device in the form of software, so that the processor can call and execute the operations corresponding to each module.

[0174] In one embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as follows: Figure 10 As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, abbreviated as I / O) and a communication interface. The processor, memory and input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a task processing method is implemented.

[0175] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 11 As shown. The computer device includes a processor, memory, input / output interface, communication interface, display unit and input device. The processor, memory and input / output interface are connected via a system bus, and the communication interface, display unit and input device are connected to the system bus via the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner. The wireless manner can be achieved through WIFI, a mobile cellular network, NFC (near field communication) or other technologies. When the computer program is executed by the processor, a task processing method is implemented. The display unit of the computer device is used to form a visually visible image, and can be a display screen, a projection device or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen. The input device of the computer device can be a touch layer covering the display screen, or a button, trackball or touchpad set on the computer device casing, or an external keyboard, touchpad or mouse, etc.

[0176] Those skilled in the art will understand that Figure 10 、 Figure 11 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.

[0177] In one embodiment, a computer device is provided, including a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, steps of a task processing method are implemented, for example:

[0178] Obtain an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data;

[0179] Calling a parallel processing interface function through the main thread, creating a thread pool object through the parallel processing interface function, the thread pool object including a task scheduler and multiple worker threads;

[0180] Through the task scheduler, the elements in the slice data are traversed in sequence, and corresponding work tasks are generated according to the processing functions for the traversed elements to obtain multiple work tasks;

[0181] The task scheduler sends the multiple work tasks to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, which are then sent back to the main thread.

[0182] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the task processing method are implemented, for example:

[0183] Obtain an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data;

[0184] Calling a parallel processing interface function through the main thread, creating a thread pool object through the parallel processing interface function, the thread pool object including a task scheduler and multiple worker threads;

[0185] Through the task scheduler, the elements in the slice data are traversed in sequence, and corresponding work tasks are generated according to the processing functions for the traversed elements to obtain multiple work tasks;

[0186] The task scheduler sends the multiple work tasks to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, which are then sent back to the main thread.

[0187] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps of a task processing method, for example:

[0188] Obtain an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data;

[0189] Calling a parallel processing interface function through the main thread, creating a thread pool object through the parallel processing interface function, the thread pool object including a task scheduler and multiple worker threads;

[0190] Through the task scheduler, the elements in the slice data are traversed in sequence, and corresponding work tasks are generated according to the processing functions for the traversed elements to obtain multiple work tasks;

[0191] The task scheduler sends the multiple work tasks to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, which are then sent back to the main thread.

[0192] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions.

[0193] Those skilled in the art will appreciate that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the above-mentioned embodiments. In particular, any reference to memory, database, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The databases involved in the various embodiments provided herein may include at least one of a relational database and a non-relational database. Non-relational databases may include, but are not limited to, distributed databases based on blockchains. The processors involved in the various embodiments provided herein may be, but are not limited to, general-purpose processors, central processing units (CPUs), graphics processing units (GPUs), digital signal processors (DSPs), programmable logic devices (PLDs), data processing logic devices based on quantum computing, and the like.

[0194] The technical features of the above embodiments can be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0195] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present application. It should be noted that a person of ordinary skill in the art may make various modifications and improvements without departing from the spirit of the present application, and these modifications and improvements fall within the scope of protection of the present application. Therefore, the scope of protection of the present application shall be determined by the appended claims.< / atomicbool> < / job> < / job> < / worker> < / job> < / worker>

Claims

1. A task processing method, characterized in that: The method comprises: Obtaining an iterative task through the main thread, the iterative task including slice data and a processing function for each element in the slice data; Calling a parallel processing interface function through the main thread, creating a thread pool object through the parallel processing interface function, wherein the thread pool object includes a task scheduler and multiple worker threads; By means of the task scheduler, the elements in the slice data are sequentially traversed, and corresponding work tasks are generated according to the processing functions for the traversed elements, thereby obtaining a plurality of work tasks; The task scheduler sends the multiple work tasks to the multiple work threads, and the multiple work threads execute the multiple work tasks in parallel to obtain execution results, which are then sent back to the main thread.

2. The method according to claim 1, characterized in that The task scheduler sequentially traverses the elements in the slice data and generates corresponding work tasks according to the processing functions for the traversed elements to obtain multiple work tasks, including: Determining, by the task scheduler, a data processing amount of each working thread according to the data length of the slice data and the number of threads of the multiple working threads; traversing the elements in the slice data in sequence, and dividing the elements in the slice data into a plurality of data blocks according to the data processing amount; Corresponding work tasks are generated according to the processing function for the elements included in each data block, thereby obtaining a plurality of work tasks.

3. The method according to claim 1, characterized in that The multiple work threads are respectively used to process work tasks of different priority levels, and sending the multiple work tasks to the multiple work threads includes: Get the task priority corresponding to each work task; According to the task priority, the corresponding work task is sent to the work thread for processing the work task of the corresponding priority level.

4. The method according to claim 1, wherein The method further comprises: Creating a channel, where the channel is used by the multiple worker threads to send execution results to the main thread; The execution results of each of the working threads are collected through the main thread.

5. The method according to claim 1, wherein The method further comprises: Creating a task double-ended queue for each of the plurality of working threads, wherein the task double-ended queue includes a task taking end and a task stealing end; The sending of the plurality of work tasks to the plurality of work threads, and having the plurality of work threads execute the plurality of work tasks in parallel to obtain execution results, includes: Adding the plurality of work tasks to the task double-ended queues corresponding to the plurality of work threads; Through the working thread, the working task is taken out from the task taking end of the task double-ended queue corresponding to the working thread, and the taken out working task is executed.

6. The method according to claim 5, characterized in that The method further comprises: The task scheduler traverses the task stealing end of the task double-ended queue corresponding to each working thread, and when a working task exists at the traversed task stealing end, the working task is taken out and allocated to the working thread that is currently in an idle state.

7. The method according to claim 1, characterized in that The method further comprises: When the task double-ended queue corresponding to the worker thread is empty, confirming that the worker thread is currently in an idle state; When the task double-ended queue corresponding to the working thread is not empty, it is confirmed that the working thread is currently in a non-idle state.

8. The method according to claim 1, characterized in that The creating of the thread pool object through the parallel processing interface function includes: Obtaining the number of processor cores through the parallel processing interface function, and determining the number of threads according to the number of processor cores, wherein the number of threads is less than the number of processor cores; A plurality of working threads are created according to the number of threads through the parallel processing interface function.

9. The method according to any one of claims 1 to 8, characterized in that The method further comprises: Obtaining an original data set and a processing function for each element in the original data set through the main thread; The original data set is converted into slice data of an atomic reference counting type, and the slice data and the processing function constitute the iterative task.

10. A task processing device, characterized in that: The device comprises: An iterative task acquisition module, configured to acquire an iterative task through a main thread, wherein the iterative task includes slice data and a processing function for each element in the slice data; A parallel processing calling module is used to call a parallel processing interface function through the main thread, and create a thread pool object through the parallel processing interface function, wherein the thread pool object includes a task scheduler and multiple worker threads; A task division module is used to traverse the elements in the slice data in sequence through the task scheduler, and generate corresponding work tasks according to the processing functions for the traversed elements to obtain multiple work tasks; The task parallel module is used to send the multiple work tasks to the multiple work threads through the task scheduler, and the multiple work threads execute the multiple work tasks in parallel, obtain execution results, and send the execution results back to the main thread.

11. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 9 are implemented.

12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.

13. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.