A single-threaded multitasking execution method, device, equipment and medium

CN115576671BActive Publication Date: 2026-10-20INSPUR SUZHOU INTELLIGENT TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202211280347.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-10-19
Publication Date
2026-10-20
Estimated Expiration
2042-10-19

AI Technical Summary

Technical Problem

但是纤程的执行一般都为同步的,对于拥有大量不同类型任务的系统来说,每一个任务都需要发起一个纤程,这样造成的等待时间开销同样不少,尤其是对存储这样的智能设备来说,高性能的存储设备每秒钟需要响应上万的io设备,而每个io设备在不同的阶段,又需要发起不同的处理任务;即需要大量并且及时的响应任务,所以这里只靠同步的纤程是不可行的

Benefits of technology

[0075]本发明实施例提供的技术方案相比多线程技术,更简单易实现,且可以满足高性能设备的要求,提高了任务的响应速度;同时能够代替纤程,满足高响应低延迟设备的高性能要求,避免目前纤程同步机制带来的损耗。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115576671B_ABST
    Figure CN115576671B_ABST
Patent Text Reader

Abstract

The application discloses a single-thread multi-task execution method and device, equipment and a medium, and belongs to the technical field of computers. The method comprises the following steps: creating a to-be-executed task in a task unit, setting a task state value of the to-be-executed task as a first threshold value; initializing a task state and a callback function of the to-be-executed task; scheduling the initialized to-be-executed task, and putting the scheduled to-be-executed task into a task pool; and a working thread acquires the to-be-executed task in the task pool and cyclically executes the to-be-executed task in the task pool. Compared with the multi-thread technology, the application is simpler and easier to implement, and can meet the requirements of high-performance equipment. Meanwhile, the application can replace a fiber, meet the high-performance requirements of high-response and low-delay equipment, and avoid the loss caused by the current fiber synchronization mechanism.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a single-threaded multi-tasking execution method, apparatus, device, and medium. Background Technology

[0002] In the current mobile internet era, most mainstream operating systems utilize a multi-threaded model to achieve multitasking. Typically, multiple threads within a device compete to execute various tasks, taking turns using the CPU to perform their respective tasks, thus achieving macroscopic concurrency. However, this switching between threads involves transitions between kernel mode and user mode, resulting in significant time overhead and a substantial impact on device performance. This time overhead is unsuitable for high-response, low-latency, high-concurrency scenarios.

[0003] A fiber is a lightweight thread, a type of user thread, that allows applications to independently decide how their threads should operate. Applications can create multiple fibers within a single thread and schedule them among themselves. Due to their low overhead and fast switching, systems can typically initiate tens of thousands of fibers simultaneously, mitigating the overhead of thread switching. However, fiber execution is generally synchronous. For systems with a large number of different types of tasks, each task requires initiating a fiber, resulting in significant waiting time overhead. This is especially true for intelligent devices like storage, where high-performance storage devices need to respond to tens of thousands of I / O requests per second, and each I / O request needs to initiate different processing tasks at different stages; i.e., a large number of tasks requiring timely responses are needed. Therefore, relying solely on synchronous fibers is not feasible. Summary of the Invention

[0004] To overcome the aforementioned technical deficiencies, the present invention aims to provide a single-threaded multi-task execution method, apparatus, device, and medium. The single-threaded multi-task execution method of the present invention is applied to a single-threaded multi-task execution system, which includes task units, worker threads, and a task pool. The task unit includes a task state subunit, a callback function subunit, and an execution count subunit; the task pool includes a pending execution pool and a waiting pool. The method includes: creating pending tasks in the task unit and setting the task state value of the pending tasks to a first threshold; initializing the task state and callback function of the pending tasks; scheduling the initialized pending tasks and placing the scheduled pending tasks into the task pool; and the worker thread acquiring pending tasks from the task pool and executing them cyclically. Compared to multi-threading technology, the present invention is simpler and easier to implement, and can meet the requirements of high-performance devices; it can also replace fibers, meeting the high-performance requirements of high-response, low-latency devices and avoiding the losses caused by current fiber synchronization mechanisms.

[0005] The specific technical solutions provided by the embodiments of the present invention are as follows:

[0006] Firstly, a single-threaded multi-task execution method is provided. This method is applied to a single-threaded multi-task execution system, which includes task units, worker threads, and a task pool. The task unit includes a task state subunit, a callback function subunit, and an execution count subunit. The task pool includes a pending execution pool and a waiting pool. The method includes:

[0007] In the task unit, a task to be executed is created, and the task status value of the task to be executed is set to a first threshold.

[0008] Initialize the task status and callback function of the task to be executed;

[0009] The initialized tasks to be executed are scheduled, and the scheduled tasks are placed into the task pool;

[0010] The worker thread retrieves the tasks to be executed from the task pool and executes them in a loop.

[0011] Furthermore, the initialization of the task state and callback function of the task to be executed includes:

[0012] Determine whether the task state of the task to be executed needs to be initialized;

[0013] If yes, then set the task status value of the task to be executed to the second threshold; if no, then determine whether the callback function of the task to be executed has been imported into the callback function subunit.

[0014] The step of determining whether the callback function of the task to be executed has been imported into the callback function subunit includes:

[0015] Determine whether the callback function of the task to be executed has been imported into the callback function subunit;

[0016] If yes, then update the callback function of the task to be executed in the callback function subunit; otherwise, end the process.

[0017] Furthermore, the step of scheduling the initialized tasks to be executed and placing the scheduled tasks into the task pool includes:

[0018] Determine whether the task status value of the task to be executed is equal to the second threshold;

[0019] If yes, then determine whether there is an empty slot in the execution pool; if no, then determine whether the task status value of the task to be executed is the third threshold.

[0020] The step of determining whether there are empty slots in the pool to be executed includes:

[0021] Determine if there are any empty slots in the pool to be executed;

[0022] If yes, the task to be executed is placed in the task waiting pool, and the task status value of the task to be executed is set to the third threshold; if no, the task to be executed is placed in the waiting pool.

[0023] Further, the determination of whether the task status value of the task to be executed is the third threshold includes:

[0024] Determine whether the task status value of the task to be executed is the third threshold;

[0025] If yes, then the number of times the task to be executed needs to be executed is increased by one in the execution count subunit; otherwise, the process ends.

[0026] Furthermore, the worker thread obtains tasks to be executed from the task pool and executes the tasks to be executed from the task pool in a loop, including:

[0027] Determine whether there are any tasks to be executed in the execution pool;

[0028] If yes, then retrieve the tasks to be executed from the task pool, reset the task status value of the task to be executed to the second threshold, and execute the callback function of the task to be executed to determine whether the number of times the task to be executed is equal to the fourth threshold; if no, then end the process.

[0029] Further, the determination of whether the number of times the task to be executed is equal to the fourth threshold includes:

[0030] Determine whether the number of times the task to be executed has reached the fourth threshold.

[0031] If yes, then determine whether the waiting pool is empty; if no, then determine whether the execution time of the task to be executed is greater than the fifth threshold.

[0032] The determination of whether the waiting pool is empty includes:

[0033] Determine whether the waiting pool is empty;

[0034] If yes, then the current execution of the task to be executed ends, and the process continues to determine whether there are any tasks to be executed in the task pool; if no, then the tasks to be executed in the waiting pool are put into the task pool, and the tasks to be executed in the task pool are executed in a loop.

[0035] Further, the determination of whether the execution time of the task to be executed is greater than the fifth threshold includes:

[0036] Determine whether the execution time of the task to be executed is greater than the fifth threshold;

[0037] If yes, the task to be executed is rescheduled and placed in the waiting pool; otherwise, the callback function of the task to be executed is executed.

[0038] Secondly, a single-threaded multi-task execution device is provided, the device comprising:

[0039] A creation module is used to create a task to be executed in the task unit and set the task status value of the task to be executed to a first threshold.

[0040] An initialization module is used to initialize the task state and callback functions of the task to be executed;

[0041] The scheduling module is used to schedule the tasks to be executed after initialization and put the scheduled tasks into the task pool.

[0042] The execution module is used to obtain the tasks to be executed from the task pool and execute the tasks to be executed from the task pool in a loop.

[0043] Thirdly, a computer device is provided, the device comprising:

[0044] A memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the steps of the single-threaded multitasking execution method as described in any of the first aspects.

[0045] Fourthly, a computer storage medium is provided, the medium comprising:

[0046] It stores a computer program that, when executed by the memory, implements the steps of the single-threaded multitasking execution method described in any of the first aspects.

[0047] Compared with existing technologies, the method of the technical solution provided in this invention includes: creating a task to be executed in the task unit, setting the task status value of the task to be executed to a first threshold; initializing the task status and callback function of the task to be executed; scheduling the initialized task to be executed, and placing the scheduled task to be executed into the task pool; the worker thread obtaining the task to be executed from the task pool and executing the task to be executed in the task pool in a loop. Compared with multithreading technology, this invention is simpler and easier to implement, and can meet the requirements of high-performance devices, improving the response speed of tasks; at the same time, it can replace fibers, meet the high-performance requirements of high-response, low-latency devices, and avoid the losses caused by the current fiber synchronization mechanism.

[0048] The technical solution provided by the embodiments of the present invention ensures the stability and robustness of the program while rapidly achieving the requirements of multi-tasking functionality, without the risks and overhead of multi-threaded concurrency. By using this method, programmers can achieve the purpose of multi-tasking without performing multi-threaded programming, following the technical solution described above. Attached Figure Description

[0049] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0050] Figure 1 This is a flowchart of a single-threaded multi-task execution method provided in Embodiment 1 of the present invention;

[0051] Figure 2 This is a structural diagram of the task unit provided in Embodiment 1 of the present invention;

[0052] Figure 3 This is a structural diagram of the task pool provided in Embodiment 1 of the present invention;

[0053] Figure 4 This is a first flowchart of the single-threaded multi-task execution method provided in Embodiment 2 of the present invention;

[0054] Figure 5 This is a second flowchart of the single-threaded multi-task execution method provided in Embodiment 2 of the present invention;

[0055] Figure 6 This is the third flowchart of the single-threaded multi-task execution method provided in Embodiment 2 of the present invention;

[0056] Figure 7 This is a structural diagram of the single-threaded multi-task execution device provided in Embodiment 3 of the present invention;

[0057] Figure 8 This is an exemplary system provided in Embodiment 5 of the present invention, which can be used to implement the various embodiments described in this application. Detailed Implementation

[0058] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of the embodiments of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this invention, and not all embodiments. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this invention.

[0059] It should be noted that, unless the context explicitly requires it, the words "comprising," "including," and similar terms in the entire specification and claims should be interpreted as encompassing rather than being exclusive or exhaustive; that is, meaning "including but not limited to."

[0060] Furthermore, in the description of this invention, unless otherwise stated, "a plurality of" means two or more.

[0061] Example 1

[0062] This invention provides a single-threaded multi-task execution method, such as... Figure 1 As shown, the single-threaded multi-task execution method is applied to a single-threaded multi-task execution system, which includes task units, worker threads, and a task pool, such as... Figure 2 As shown, the task unit includes a task status subunit, a callback function subunit, and an execution count subunit; as Figure 3 As shown, the task pool includes a task pool to be executed and a waiting pool, and the method includes:

[0063] In the task unit, a task to be executed is created, and the task status value of the task to be executed is set to a first threshold.

[0064] Initialize the task status and callback function of the task to be executed;

[0065] The initialized tasks to be executed are scheduled, and the scheduled tasks are placed into the task pool;

[0066] The worker thread retrieves the tasks to be executed from the task pool and executes them in a loop.

[0067] Specifically, the present invention provides a single-threaded multi-task execution method, which is suitable for devices with a large number of different types of tasks and high performance requirements, such as storage devices.

[0068] The single-threaded multi-task execution method is applied to a single-threaded multi-task execution system, which includes task units, worker threads, and task pools.

[0069] The task is initiated by the system, that is, the task that needs to be executed, such as... Figure 2 As shown, the task unit includes a task status subunit, a callback function subunit, and an execution count subunit. The callback function subunit records the callback functions that the task needs to execute and is the main body of the task.

[0070] In addition, task status and number of repetitions are both attributes of a task and can take other forms; only one embodiment is shown here. The task status is used to record the task's state during the scheduling process. In this embodiment, the task status value is a pointer, which can be used not only for task scheduling but also to identify the task's state.

[0071] The execution count subunit records the number of times the task needs to be executed. Because tasks may be called repeatedly by the system in the case of asynchronous scheduling, when the same task is repeatedly initiated, it is only necessary to increment the execution count recorded by the task's execution count subunit by 1, without rescheduling the task, thus ensuring the robustness of the system code.

[0072] like Figure 3 As shown, the task pool refers to a queue that stores tasks. The task pool includes a pending execution pool and a waiting pool. The pending execution pool stores tasks that are about to be executed during scheduling and can be implemented using a data structure that facilitates data storage. In this embodiment, the pending execution pool is a circular queue, a data structure that facilitates data storage. However, the number of tasks that the pending execution pool can hold is generally limited, so a waiting pool is introduced here. The function of the waiting pool is to place tasks into the waiting pool after the pending execution pool is full. The implementation of the waiting pool requires a data structure that can store an unlimited amount of data. In this embodiment, the waiting pool is a singly linked list.

[0073] The worker thread refers to the thread that actually executes the task; the task pool is bound to the worker thread, and the worker thread executes the tasks in the task pool by accessing the task pool.

[0074] The beneficial effects of the technical solution provided by the embodiments of the present invention are as follows:

[0075] Compared with multi-threading technology, the technical solution provided by the embodiments of the present invention is simpler and easier to implement, and can meet the requirements of high-performance devices, improving the response speed of tasks; at the same time, it can replace fiber, meet the high-performance requirements of high-response and low-latency devices, and avoid the losses caused by the current fiber synchronization mechanism.

[0076] The technical solution provided by the embodiments of the present invention ensures the stability and robustness of the program while rapidly achieving the requirements of multi-tasking functionality, without the risks and overhead of multi-threaded concurrency. By using this method, programmers can achieve the purpose of multi-tasking without performing multi-threaded programming, following the technical solution described above.

[0077] Example 2

[0078] This invention provides a single-threaded multi-task execution method, such as... Figure 1 As shown, the single-threaded multi-task execution method is applied to a single-threaded multi-task execution system. The single-threaded multi-task execution system includes task units, worker threads, and a task pool. The task unit includes a task state subunit, a callback function subunit, and an execution count subunit. The task pool includes a waiting pool and a pending pool. The method includes:

[0079] Step S01: Create a task to be executed in the task unit and set the task status value of the task to be executed to a first threshold.

[0080] Specifically, in the task unit, a task to be executed is created, the status pointer is set to the NULL pointer, and the task status value of the task to be executed is set to the first threshold. At this time, task scheduling cannot be performed, and the task is in a protected state. The callback function subunit stores the callback functions that the task to be executed needs to execute. When the number of repeated executions recorded in the execution count unit of the task to be executed is 0, it means that this task does not need to be executed repeatedly.

[0081] Wherein, the first threshold is 1.

[0082] Step S02: Initialize the task status and callback function of the task to be executed.

[0083] Specifically, by pointing the status pointer of the task to be executed to the task itself, the task status value of the task to be executed is set to a second threshold, that is, the task to be executed can be scheduled in this state; the callback function of the task to be executed is initialized, and the callback function of the task to be executed can be updated in the callback function sub-unit.

[0084] The second threshold is 2.

[0085] Step S02 also includes:

[0086] Step S021: Determine whether the task status of the task to be executed needs to be initialized;

[0087] If yes, then set the task status value of the task to be executed to the second threshold; if no, then determine whether the callback function of the task to be executed has been imported into the callback function subunit.

[0088] The step of determining whether the callback function of the task to be executed has been imported into the callback function subunit includes:

[0089] Determine whether the callback function of the task to be executed has been imported into the callback function subunit;

[0090] If yes, then update the callback function of the task to be executed in the callback function subunit; otherwise, end the process.

[0091] Specifically, such as Figure 4 As shown, a. First, determine whether the task status of the task to be executed needs to be initialized. If so, continue to step b; otherwise, skip to step c.

[0092] b. Point the status pointer to the task itself, and set the task status value of the task to be executed to the second threshold, that is, the task to be executed can be scheduled in this state;

[0093] c. Determine whether the callback function of the task to be executed has been imported into the callback function subunit. If it has been imported, continue to step d; otherwise, end the process.

[0094] d. Update the callback function of the task to be executed in the callback function subunit.

[0095] Step S03: Schedule the initialized tasks to be executed and put the scheduled tasks into the task pool.

[0096] Step S03 also includes:

[0097] Step S031: Determine whether the task status value of the task to be executed is the second threshold.

[0098] If yes, then determine whether there is an empty slot in the execution pool; if no, then determine whether the task status value of the task to be executed is the third threshold.

[0099] The step of determining whether there are empty slots in the pool to be executed includes:

[0100] Determine if there are any empty slots in the pool to be executed;

[0101] If yes, the task to be executed is placed in the task waiting pool, and the task status value of the task to be executed is set to the third threshold; if no, the task to be executed is placed in the waiting pool.

[0102] Step S032: Determine whether the task status value of the task to be executed is the third threshold.

[0103] If yes, then the number of times the task to be executed needs to be executed is increased by one in the execution count subunit; otherwise, the process ends.

[0104] Specifically, such as Figure 5 As shown, a. Determine whether the task status value of the task to be executed is the second threshold, that is, in a schedulable state; if yes, proceed to step d; otherwise, execute step b.

[0105] b. Determine whether the task status value of the task to be executed is the third threshold. If yes, continue to step c; otherwise, end the process.

[0106] c. Increment the number of times the task to be executed is to be executed by 1, and then the scheduling ends.

[0107] d. Determine if there are any empty slots in the execution pool; if there are empty slots, proceed to step e; if there are no empty slots, proceed to step f.

[0108] e. Place the task to be executed into the execution pool. In this embodiment, the task to be executed is placed at the tail of the circular queue, and the status pointer of the task to be executed is set to the corresponding position in the circular queue. The task status value of the task to be executed is set to the third threshold, which means that the task to be executed is already in the queue.

[0109] f. Place the task to be executed into the waiting pool. In this embodiment, the task to be executed is inserted into the tail of the singly linked list.

[0110] The third threshold is 3.

[0111] Step S04: The worker thread obtains the tasks to be executed from the task pool and executes the tasks to be executed from the task pool in a loop.

[0112] Specifically, the worker thread continuously accesses the task pool, retrieves the tasks to be executed from the task pool, and executes the tasks.

[0113] Step S04 also includes:

[0114] Step S041: Determine whether there are any tasks to be executed in the execution pool;

[0115] If yes, then retrieve the tasks to be executed from the task pool, reset the task status value of the task to be executed to the second threshold, and execute the callback function of the task to be executed to determine whether the number of times the task to be executed is equal to the fourth threshold; if no, then end the process.

[0116] Step S042: Determine whether the number of times the task to be executed has reached the fourth threshold.

[0117] If yes, then determine whether the waiting pool is empty; if no, then determine whether the execution time of the task to be executed is greater than the fifth threshold.

[0118] The determination of whether the waiting pool is empty includes:

[0119] Determine whether the waiting pool is empty;

[0120] If yes, then the current execution of the task to be executed ends, and the process continues to determine whether there are any tasks to be executed in the task pool; if no, then the tasks to be executed in the waiting pool are put into the task pool, and the tasks to be executed in the task pool are executed in a loop.

[0121] Step S043: Determine whether the execution time of the task to be executed is greater than the fifth threshold.

[0122] If yes, the task to be executed is rescheduled and placed in the waiting pool; otherwise, the callback function of the task to be executed is executed.

[0123] Specifically, such as Figure 6 As shown, a. First, determine if there are any tasks to be executed in the execution pool. If so, continue execution; otherwise, end.

[0124] b. Take a task to be executed from the pool of tasks to be executed. In this embodiment, this means taking the task at the head of the circular queue.

[0125] c. Set the task's status pointer to point to the task itself, and the task's status value returns to 2, indicating that the task can be scheduled again;

[0126] d. Execute the callback function of the task to be executed;

[0127] e. Determine if the number of executions for the task to be executed is 0. If it is not 0, proceed to step f; if it is 0, proceed to step g.

[0128] f. Determine whether the execution time of the task to be executed is too long, that is, whether the execution time of the task to be executed is greater than the fifth threshold. If it is too long, reschedule the task to be executed and put the rescheduled task to be executed into the waiting pool to wait for the next execution to prevent the execution process from being blocked; if the execution time limit, i.e. the fifth threshold, is not exceeded, then jump back to step d.

[0129] g. Determine whether the waiting pool is empty. If it is empty, end the current execution of the task to be executed and jump back to step a to perform the loop. If the waiting pool is not empty, put the task to be executed in the waiting pool into the task to be executed pool. In this embodiment, the task at the head of the singly linked list is taken out and put into the tail of the circular queue. After completion, jump back to step a to perform the loop.

[0130] The fourth threshold is 0; the fifth threshold can be determined by considering factors such as the number of tasks in the task pool, the average execution time, the busyness of the task pool, and the upper limit of the number of tasks in the waiting pool. For example, if the average execution time of each task to be executed is 1 millisecond, and the upper limit of the number of tasks in the waiting pool of the task pool is 64, then the fifth threshold for execution time can be set to 64 milliseconds.

[0131] This invention establishes a task pool on the worker thread (the task pool includes, but is not limited to, data structures such as circular queues and singly linked lists). In this invention, tasks are produced through three modules: a creation module, an initialization module, and a scheduling module. The worker thread then calls the execution module to consume the tasks.

[0132] The single-threaded multi-task execution method provided in this embodiment of the invention can be improved and optimized in several ways without departing from the technical solution of this invention, and these improvements and optimizations should also be considered within the scope of protection of this invention.

[0133] The beneficial effects of the technical solution provided by the embodiments of the present invention are as follows:

[0134] Compared with multi-threading technology, the technical solution provided by the embodiments of the present invention is simpler and easier to implement, and can meet the requirements of high-performance devices, improving the response speed of tasks; at the same time, it can replace fiber, meet the high-performance requirements of high-response and low-latency devices, and avoid the losses caused by the current fiber synchronization mechanism.

[0135] The technical solution provided by the embodiments of the present invention ensures the stability and robustness of the program while rapidly achieving the requirements of multi-tasking functionality, without the risks and overhead of multi-threaded concurrency. By using this method, programmers can achieve the purpose of multi-tasking without performing multi-threaded programming, following the technical solution described above.

[0136] Example 3

[0137] This invention provides a single-threaded multi-task execution device, such as... Figure 7 As shown, the device includes a creation module, an initialization module, a scheduling module, and an execution module.

[0138] The scheduling and execution of these multiple tasks requires task producers and consumers. Task production is accomplished by system calls to the creation module, initialization module, and scheduling module; task consumption is accomplished by worker threads calling the execution module.

[0139] In this embodiment, a creation module is used to create a task to be executed in the task unit and set the task status value of the task to be executed to a first threshold.

[0140] An initialization module is used to initialize the task state and callback functions of the task to be executed;

[0141] The scheduling module is used to schedule the tasks to be executed after initialization and put the scheduled tasks into the task pool.

[0142] The execution module is used to obtain the tasks to be executed from the task pool and execute the tasks to be executed from the task pool in a loop.

[0143] Furthermore, the initialization module is used to determine whether the task state of the task to be executed needs to be initialized;

[0144] If yes, then set the task status value of the task to be executed to the second threshold; if no, then determine whether the callback function of the task to be executed has been imported into the callback function subunit.

[0145] The step of determining whether the callback function of the task to be executed has been imported into the callback function subunit includes:

[0146] Determine whether the callback function of the task to be executed has been imported into the callback function subunit;

[0147] If yes, then update the callback function of the task to be executed in the callback function subunit; otherwise, end the process.

[0148] Furthermore, the scheduling module is also used to determine whether the task status value of the task to be executed is a second threshold.

[0149] If yes, then determine whether there is an empty slot in the execution pool; if no, then determine whether the task status value of the task to be executed is the third threshold.

[0150] The step of determining whether there are empty slots in the pool to be executed includes:

[0151] Determine if there are any empty slots in the pool to be executed;

[0152] If yes, the task to be executed is placed in the task waiting pool, and the task status value of the task to be executed is set to the third threshold; if no, the task to be executed is placed in the waiting pool.

[0153] Furthermore, the scheduling module is used to determine whether the task status value of the task to be executed is a third threshold.

[0154] If yes, then the number of times the task to be executed needs to be executed is increased by one in the execution count subunit; otherwise, the process ends.

[0155] Furthermore, the execution module is also used to determine whether there are any tasks to be executed in the execution pool;

[0156] If yes, then retrieve the tasks to be executed from the task pool, reset the task status value of the task to be executed to the second threshold, and execute the callback function of the task to be executed to determine whether the number of times the task to be executed is equal to the fourth threshold; if no, then end the process.

[0157] Furthermore, the execution module is also used to determine whether the number of times the task to be executed is equal to the fourth threshold.

[0158] If yes, then determine whether the waiting pool is empty; if no, then determine whether the execution time of the task to be executed is greater than the fifth threshold.

[0159] The determination of whether the waiting pool is empty includes:

[0160] Determine whether the waiting pool is empty;

[0161] If yes, then the current execution of the task to be executed ends, and the process continues to determine whether there are any tasks to be executed in the task pool; if no, then the tasks to be executed in the waiting pool are put into the task pool, and the tasks to be executed in the task pool are executed in a loop.

[0162] Furthermore, the execution module is also used to determine whether the execution time of the task to be executed is greater than the fifth threshold.

[0163] If yes, the task to be executed is rescheduled and placed in the waiting pool; otherwise, the callback function of the task to be executed is executed.

[0164] The beneficial effects of the technical solution provided by the embodiments of the present invention are as follows:

[0165] Compared with multi-threading technology, the technical solution provided by the embodiments of the present invention is simpler and easier to implement, and can meet the requirements of high-performance devices, improving the response speed of tasks; at the same time, it can replace fiber, meet the high-performance requirements of high-response and low-latency devices, and avoid the losses caused by the current fiber synchronization mechanism.

[0166] The technical solution provided by the embodiments of the present invention ensures the stability and robustness of the program while rapidly achieving the requirements of multi-tasking functionality, without the risks and overhead of multi-threaded concurrency. By using this method, programmers can achieve the purpose of multi-tasking without performing multi-threaded programming, following the technical solution described above.

[0167] Example 4

[0168] This invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it can perform the following single-threaded multi-tasking execution method:

[0169] In the task unit, a task to be executed is created, and the task status value of the task to be executed is set to a first threshold.

[0170] Initialize the task status and callback function of the task to be executed;

[0171] The initialized tasks to be executed are scheduled, and the scheduled tasks are placed into the task pool;

[0172] The worker thread retrieves the tasks to be executed from the task pool and executes them in a loop.

[0173] The beneficial effects of the technical solution provided by the embodiments of the present invention are as follows:

[0174] The technical solution provided by the embodiments of the present invention ensures the stability and robustness of the program while rapidly achieving the requirements of multi-tasking functionality, without the risks and overhead of multi-threaded concurrency. By using this method, programmers can achieve the purpose of multi-tasking without performing multi-threaded programming, following the technical solution described above.

[0175] Example 5

[0176] This invention provides a computer storage medium, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the computer program, it performs the following steps:

[0177] In the task unit, a task to be executed is created, and the task status value of the task to be executed is set to a first threshold.

[0178] Initialize the task status and callback function of the task to be executed;

[0179] The initialized tasks to be executed are scheduled, and the scheduled tasks are placed into the task pool;

[0180] The worker thread retrieves the tasks to be executed from the task pool and executes them in a loop.

[0181] Furthermore, the initialization of the task state and callback function of the task to be executed includes:

[0182] Determine whether the task state of the task to be executed needs to be initialized;

[0183] If yes, then set the task status value of the task to be executed to the second threshold; if no, then determine whether the callback function of the task to be executed has been imported into the callback function subunit.

[0184] The step of determining whether the callback function of the task to be executed has been imported into the callback function subunit includes:

[0185] Determine whether the callback function of the task to be executed has been imported into the callback function subunit;

[0186] If yes, then update the callback function of the task to be executed in the callback function subunit; otherwise, end the process.

[0187] Furthermore, the step of scheduling the initialized tasks to be executed and placing the scheduled tasks into the task pool includes:

[0188] Determine whether the task status value of the task to be executed is equal to the second threshold;

[0189] If yes, then determine whether there is an empty slot in the execution pool; if no, then determine whether the task status value of the task to be executed is the third threshold.

[0190] The step of determining whether there are empty slots in the pool to be executed includes:

[0191] Determine if there are any empty slots in the pool to be executed;

[0192] If yes, the task to be executed is placed in the task waiting pool, and the task status value of the task to be executed is set to the third threshold; if no, the task to be executed is placed in the waiting pool.

[0193] Further, the determination of whether the task status value of the task to be executed is the third threshold includes:

[0194] Determine whether the task status value of the task to be executed is the third threshold;

[0195] If yes, then the number of times the task to be executed needs to be executed is increased by one in the execution count subunit; otherwise, the process ends.

[0196] Furthermore, the worker thread obtains tasks to be executed from the task pool and executes the tasks to be executed from the task pool in a loop, including:

[0197] Determine whether there are any tasks to be executed in the execution pool;

[0198] If yes, then retrieve the tasks to be executed from the task pool, reset the task status value of the task to be executed to the second threshold, and execute the callback function of the task to be executed to determine whether the number of times the task to be executed is equal to the fourth threshold; if no, then end the process.

[0199] Further, the determination of whether the number of times the task to be executed is equal to the fourth threshold includes:

[0200] Determine whether the number of times the task to be executed has reached the fourth threshold.

[0201] If yes, then determine whether the waiting pool is empty; if no, then determine whether the execution time of the task to be executed is greater than the fifth threshold.

[0202] The determination of whether the waiting pool is empty includes:

[0203] Determine whether the waiting pool is empty;

[0204] If yes, then the current execution of the task to be executed ends, and the process continues to determine whether there are any tasks to be executed in the task pool; if no, then the tasks to be executed in the waiting pool are put into the task pool, and the tasks to be executed in the task pool are executed in a loop.

[0205] Further, the determination of whether the execution time of the task to be executed is greater than the fifth threshold includes:

[0206] Determine whether the execution time of the task to be executed is greater than the fifth threshold;

[0207] If yes, the task to be executed is rescheduled and placed in the waiting pool; otherwise, the callback function of the task to be executed is executed.

[0208] Figure 8 This is an exemplary system provided in Embodiment 5 of the present invention that can be used to implement the various embodiments described in this application;

[0209] like Figure 8 As shown, in some embodiments, the system can function as any of the aforementioned devices for single-threaded multitasking execution in each of the various embodiments. In some embodiments, the system may include one or more computer-readable media (e.g., system memory or NVM / storage device) having a result, and one or more processors (e.g., one or more processors) coupled to the one or more computer-readable media and configured to execute the result to implement the module thereby performing the actions described in this application.

[0210] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program using related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided by this invention can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and RAMbus dynamic RAM (RDRAM), etc.

[0211] The technical features of the above embodiments can be combined in any way. For the sake of brevity, 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.

[0212] The embodiments described above are merely illustrative of several implementations of the present invention, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and these all fall within the protection scope of the present invention. Therefore, the protection scope of this invention patent should be determined by the appended claims.

Claims

1. A single-threaded multi-task execution method, characterized in that, The single-threaded multi-task execution method is applied to a single-threaded multi-task execution system, which includes task units, worker threads, and a task pool. The task unit includes a task state subunit, a callback function subunit, and an execution count subunit. The task pool includes a pending execution pool and a waiting pool. The method includes: In the task unit, a task to be executed is created, and the task status value of the task to be executed is set to a first threshold. Initialize the task status and callback function of the task to be executed; The initialized tasks to be executed are scheduled, and the scheduled tasks are placed into the task pool; The worker thread retrieves the tasks to be executed from the task pool and executes the tasks to be executed from the task pool in a loop; The step of scheduling the initialized tasks to be executed and placing the scheduled tasks into the task pool includes: Determine whether the task status value of the task to be executed is equal to the second threshold; If yes, then determine whether there is an empty slot in the execution pool; if no, then determine whether the task status value of the task to be executed is the third threshold. The step of determining whether there are empty slots in the pool to be executed includes: Determine if there are any empty slots in the pool to be executed; If yes, the task to be executed is placed in the task pool and the task status value of the task to be executed is set to the third threshold; if no, the task to be executed is placed in the waiting pool. The determination of whether the task status value of the task to be executed is at the third threshold includes: Determine whether the task status value of the task to be executed is the third threshold; If yes, then increment the number of times the task to be executed needs to be executed by one in the execution count subunit; otherwise, end the process. The callback function subunit records the callback functions that the task needs to execute, the task status value is a pointer used for task scheduling and to identify the status of the task, the execution count subunit records the number of times the task needs to be executed, and the waiting pool is a singly linked list. The worker thread obtains tasks to be executed from the task pool and executes the tasks to be executed from the task pool in a loop, including: determining whether there are any tasks to be executed in the task pool; if not, the process ends. If so, then retrieve the tasks to be executed from the task pool, reset the task status value of the task to be executed to the second threshold, and execute the callback function of the task to be executed. Determine whether the number of times the task to be executed is equal to the fourth threshold. If so, determine whether the waiting pool is empty. If not, determine whether the execution time of the task to be executed is greater than the fifth threshold. The determination of whether the waiting pool is empty includes: If yes, then the current execution of the task to be executed ends, and the process continues to determine whether there are any tasks to be executed in the task pool; if no, then the tasks to be executed in the waiting pool are put into the task pool, and the tasks to be executed in the task pool are executed in a loop. The determination of whether the execution time of the task to be executed is greater than the fifth threshold includes: If yes, the task to be executed is rescheduled and placed in the waiting pool; otherwise, the callback function of the task to be executed is executed.

2. The single-threaded multi-task execution method according to claim 1, characterized in that, The initialization of the task state and callback function of the task to be executed includes: Determine whether the task state of the task to be executed needs to be initialized; If yes, then set the task status value of the task to be executed to the second threshold; if no, then determine whether the callback function of the task to be executed has been imported into the callback function subunit. The step of determining whether the callback function of the task to be executed has been imported into the callback function subunit includes: Determine whether the callback function of the task to be executed has been imported into the callback function subunit; If yes, then update the callback function of the task to be executed in the callback function subunit; otherwise, end the process.

3. A single-threaded multi-task execution device for implementing the method as described in claim 1, characterized in that, The device includes: A creation module is used to create a task to be executed in the task unit and set the task status value of the task to be executed to a first threshold. An initialization module is used to initialize the task status and callback functions of the task to be executed; The scheduling module is used to schedule the tasks to be executed after initialization and put the scheduled tasks into the task pool. The execution module is used to obtain the tasks to be executed from the task pool and execute the tasks to be executed from the task pool in a loop.

4. A computer device, comprising a memory, a processor, and a computer program stored in the memory, characterized in that, The processor executes the computer program to implement the steps of the single-threaded multitasking execution method as described in any one of claims 1 to 2.

5. A computer storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by memory, it implements the steps of the single-threaded multitasking execution method as described in any one of claims 1 to 2.

Citation Information

Patent Citations

  • A task processing method and a task processing device

    CN109558255A

  • Task scheduling method and device, electronic equipment and computer readable medium

    CN113722056A

  • Multi-task processing method based on single-thread pool and related device

    CN114153573A