Timer task processing method, software timer and storage medium
By setting the timer to a threaded timer queue in a multi-core and multi-threaded environment and assigning the scanning work to different tasks to complete, the lock conflicts and bottlenecks of software timers during large-scale use are solved, and system performance is improved.
Patent Information
- Application Number
- CN202110709503.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-06-25
- Publication Date
- 2025-05-13
- Estimated Expiration
- 2041-06-25
AI Technical Summary
Under the multi-core and multi-threaded concurrent architecture, the setting task of the software timer and the scanning task need to access the same timer resources, resulting in lock conflicts, and the scanning work is concentrated on one task, which makes it easy to have bottlenecks when using timers on a large scale, especially in high CAPS scenarios, which has a significant impact on system performance.
By setting the timer to the threaded timer queue, the scanning work is completed by the task that sets the timer itself. The timer scanning task is only responsible for basic timeout detection, and the main timer scanning work is divided into different tasks to avoid lock conflicts.
It effectively avoids the bottleneck caused by scanning work being concentrated on one task when using timers on a large scale, improves system throughput, and is suitable for scenarios where software timers are used in a large number of multi-core and multi-threaded concurrent environments.
Smart Images

Figure CN115525402B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a timer task processing method, a software timer and a storage medium. Background Art
[0002] At present, the existing software timer implementation idea is that timer scanning and timeout are completed by a dedicated timer scanning task. Under the multi-core and multi-threaded concurrent architecture, since the timer setting task and the timer scanning task need to access the same timer resources, there are many lock conflicts between multiple timer setting tasks and between the timer setting task and the timer scanning task, and the scanning work is concentrated on one task. This can easily lead to bottlenecks when timers are used on a large scale, especially in high CAPS (Call Attempts Per Second) scenarios, the impact on system performance is very obvious. Summary of the invention
[0003] The purpose of the present invention is to solve at least one of the technical problems existing in the prior art, and to provide a timer task processing method, a software timer and a storage medium, which can avoid the bottleneck caused by the concentration of scanning work on one task when the timer is used on a large scale.
[0004] In a first aspect, an embodiment of the present invention provides a method for processing a timer task, comprising:
[0005] The first task is provided with a threaded first timer queue. When the first task sets a timer, the timer is set to the first timer queue, and the earliest timer in the first task is set to the timer scanning task;
[0006] If the timer scanning task detects that the earliest timer in the first task has expired, sending a timer expiration notification to the first task;
[0007] The first task scans the first timer queue according to the expiration notification of the timer.
[0008] In a second aspect, an embodiment of the present invention provides a software timer, including a timer setting module and a timer scanning module, wherein:
[0009] The timer setting module is used to set the timer set by the first task to the threaded first timer queue of the first task, and set the earliest timer in the first task to the timer scanning task;
[0010] The timer scanning module is used to detect whether the earliest timer in the first task has expired, and to send a timer expiration notification to the first task when the earliest timer in the first task has expired; and to scan the first timer queue when the first task receives the timer expiration notification.
[0011] In the third aspect, an embodiment of the present invention provides an operation control device, comprising at least one control processor and a memory for communicating with the at least one control processor; the memory stores instructions that can be executed by the at least one control processor, and the instructions are executed by the at least one control processor so that the at least one control processor can execute the timer task processing method described in the embodiment of the first aspect above.
[0012] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the timer task processing method as described in the embodiment of the first aspect above.
[0013] The embodiment of the present invention includes: a method for processing timer tasks, a software timer, an operation control device, and a computer-readable storage medium. According to the solution provided by the embodiment of the present invention, the first task sets the timer to a threaded first timer queue, and the scanning work of the first timer queue is performed by the first task itself that sets the timer. The timer scanning task only plays a basic timeout role, and the main timer scanning work is allocated to different tasks to avoid the bottleneck caused by the scanning work being concentrated on one task when the timer is used on a large scale. The threaded idea is adopted to avoid the problem of software timer lock conflict, thereby improving the system throughput, and is suitable for scenarios where the system uses a large number of software timers in a multi-core multi-threaded concurrent environment.
[0014] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained by the structures particularly pointed out in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] The accompanying drawings are used to provide a further understanding of the technical solution of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the technical solution of the present invention and do not constitute a limitation on the technical solution of the present invention.
[0016] The present invention is further described below in conjunction with the accompanying drawings and embodiments;
[0017] Figure 1It is an overall block diagram of a timer task processing method provided by an embodiment of the present invention;
[0018] Figure 2 It is a principle block diagram of a method for processing a timer task provided by an embodiment of the present invention;
[0019] Figure 3 is a flowchart of the software timer from timer setting to timeout provided by an embodiment of the present invention;
[0020] Figure 4 is a schematic diagram of the structure of a software timer provided by an embodiment of the present invention;
[0021] Figure 5 It is a structural schematic diagram of the operation control device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0022] This section will describe in detail the specific embodiments of the present invention. The preferred embodiments of the present invention are shown in the accompanying drawings. The purpose of the accompanying drawings is to supplement the description of the text part of the specification with graphics, so that people can intuitively and vividly understand each technical feature and the overall technical solution of the present invention, but it cannot be understood as a limitation on the scope of protection of the present invention.
[0023] In the description of the present invention, if there is a description of first and second, it is only for the purpose of distinguishing the technical features, and cannot be understood as indicating or implying the relative importance or implicitly indicating the number of the indicated technical features or implicitly indicating the order of the indicated technical features.
[0024] In the description of the present invention, unless otherwise clearly defined, terms such as setting, installing, connecting, etc. should be understood in a broad sense, and technicians in the relevant technical field can reasonably determine the specific meanings of the above terms in the present invention based on the specific content of the technical solution.
[0025] The embodiment of the present invention provides a timer task processing method, a software timer and a storage medium, which can avoid the bottleneck caused by the concentration of scanning work on one task when the timer is used on a large scale.
[0026] The embodiments of the present invention are further described below in conjunction with the accompanying drawings.
[0027] Reference Figure 1 and Figure 2 , Figure 1 It is an overall block diagram of a timer task processing method provided by an embodiment of the present invention;
[0028] Figure 2 It is a principle block diagram of a timer task processing method provided by an embodiment of the present invention.
[0029] A first aspect of the present invention provides a method for processing a timer task, comprising:
[0030] The first task is provided with a threaded first timer queue. When the first task sets a timer, the timer is set to the first timer queue, and the earliest timer in the first task is set to the timer scanning task;
[0031] If the timer scanning task detects that the earliest timer in the first task has expired, sending a timer expiration notification to the first task;
[0032] The first task scans the first timer queue according to the expiration notification of the timer.
[0033] According to the timer task processing method provided by an embodiment of the present invention, the first task sets the timer to a threaded first timer queue, and the scanning work of the first timer queue is performed by the first task itself that sets the timer. The timer scanning task only plays a basic timeout role, and the main timer scanning work is allocated to different tasks to complete, so as to avoid the bottleneck caused by the scanning work being concentrated on one task when the timer is used on a large scale. The threading idea is adopted to avoid the problem of software timer lock conflict, thereby improving the system throughput. The method is suitable for scenarios in which the system uses a large number of software timers in a multi-core and multi-threaded concurrent environment.
[0034] In the above-mentioned timer task processing method, the second task is provided with a threaded second timer queue. When the second task sets a timer, the timer is set to the second timer queue, and the earliest timer in the second task is set to the timer scanning task; the timer scanning task is provided with a timer array, and the timer array stores the earliest timer of the first task and the earliest timer of the second task.
[0035] It is understandable that there may be a third task, a fourth task and more tasks. The methods used by these tasks to process timer tasks are the same as those of the first task and the second task. These tasks are all provided with their own timer queues. When setting a timer, the timer is set to its own timer queue, and the earliest timer in its own timer queue is set to the timer array of the timer scanning task. The timer array of the timer scanning task stores the earliest timers of each task. When the timer scanning task detects that the earliest timer of a task has expired, a timer expiration notification is sent to the task, so that the task scans its own timer queue according to the received timer expiration notification.
[0036] In the above-mentioned method for processing timer tasks, the timer scanning task polls and detects the earliest expired timer of all tasks in the timer array.
[0037] The timer array of the timer scanning task can store the earliest timer of the first task, and may also store the earliest timers of multiple other tasks. That is, the timer array may store the earliest timers of multiple tasks. By querying the earliest timers of all tasks in the timer array, it is possible to detect one by one whether the earliest timers of multiple tasks have expired. If so, a time notification is sent to the corresponding task.
[0038] Reference Figure 2 In the above-mentioned timer task processing method, the first task is also provided with a duplication checking queue for checking duplication of the timer set to the first timer queue, and the duplication checking queue adopts a red-black tree data structure.
[0039] By setting up a duplicate checking queue to check the repeated timers, and using a red-black tree data structure to organize the duplicate checking queue, it can achieve a balance between the two operations of searching and adding and deleting, with fast speed and low resource usage.
[0040] In the above-mentioned method for processing timer tasks, the first timer queue adopts a red-black tree data structure, a binary tree data structure, a multi-fork tree data structure, an array data structure or a linked list data structure.
[0041] It is understandable that the second timer queue of the second task and the timer queues of other tasks can also adopt a red-black tree data structure, a binary tree data structure, a multi-fork data structure, an array data structure or a linked list data structure, and this application does not limit it.
[0042] Refer to the following Figure 3 The processing method of the above timer task is introduced in detail. Figure 3 It is a workflow diagram of the software timer provided by an embodiment of the present invention from timer setting to timeout.
[0043] The user of the first task sets a timer, first checks whether the timer is set repeatedly through the duplicate checking queue, then sets the timer to the threaded first timer queue of the first task, and determines whether the timer is the earliest expiring timer of the first task. If so, the earliest expiring timer of the first task is set to the timer array of the timer scanning task; the timer scanning task detects whether the earliest expiring timer of the first task has expired, and if so, sends a timeout notification to the first task; the first task starts scanning its own first timer queue, and sends a timer timeout notification to the user who set the timer of the first task, so that the user can handle the timer timeout message.
[0044] Reference Figure 4 , Figure 4 It is a schematic diagram of the structure of a software timer provided by an embodiment of the present invention.
[0045] The second aspect of the present invention provides a software timer, including a timer setting module 410 and a timer scanning module 420, wherein:
[0046] The timer setting module 410 is used to set the timer set for the first task to the threaded first timer queue of the first task, and to set the earliest timer in the first task to the timer scanning task. The timer queue is organized using a red-black tree data structure. In addition, there is also a duplicate check queue organized using a red-black tree data structure, which is used to check the duplicated timers. The red-black tree data structure can have the balance of both search and addition and deletion operations, with high speed and low resource usage.
[0047] The timer scanning module 420 includes two parts. The first part is used to detect whether the earliest timer in the first task has expired, and send a timer expiration notification to the first task when the earliest timer in the first task has expired; the second part is used to scan the first timer queue when the first task receives the timer expiration notification. The whole process adopts threading technology without locking, and adopts high-efficiency algorithms such as red-black tree / hash, thereby improving performance.
[0048] It should be noted that the red-black tree / hash data structure algorithm used in this embodiment is not the only algorithm to implement this embodiment. Other data structure algorithms such as ordinary binary trees, multi-fork trees, arrays, linked lists, etc. can be used to implement this embodiment.
[0049] The user of the first task uses the interface provided by the timer setting module 410 provided in this embodiment to set the timer, sets the timer task to the threaded timer queue of the first task, and determines whether the timer is the earliest timer of the first task. If so, the timer is set to the timer scanning task. When the timer scanning task detects that the earliest timer of the first task has expired, it notifies the first task, and the first task starts scanning its own timer queue and sends a timeout notification to the user who set the timer, and the user processes the timer timeout notification. The entire process accesses threaded data resources without lock overhead, thereby improving system throughput. At the same time, in this process, the use of related timer resources is dynamically expanded and contracted to avoid large-scale occupation of system resources when a large number of timers are used.
[0050] Compared with other software timer implementation methods, the timer task processing method and software timer of the embodiment of the present invention can achieve lock-free operation throughout the process in a multi-core multi-threaded concurrent architecture, avoid lock overhead, improve system performance, and the main timer work is shared by multiple tasks to avoid bottlenecks caused by concentrating on one task, which is conducive to the large-scale use of timers. At the same time, the use of related timer resources is dynamically scalable to avoid large-scale occupation of system resources when a large number of timers are used. It is suitable for scenarios where the system uses a large number of software timers in a multi-core multi-threaded concurrent environment.
[0051] Reference Figure 5 , Figure 5 It is a structural diagram of an operation control device 500 provided in an embodiment of the present invention.
[0052] The third aspect embodiment of the present invention provides an operation control device 500, comprising at least one control processor 510 and a memory 520 for communicating with the at least one control processor 510; the memory 520 stores instructions that can be executed by the at least one control processor 510, and the instructions are executed by the at least one control processor 510 so that the at least one control processor 510 can execute the timer task processing method described in the first aspect embodiment above.
[0053] According to the operation control device 500 provided by an embodiment of the present invention, the first task sets the timer to a threaded first timer queue, and the scanning work of the first timer queue is performed by the first task itself that sets the timer. The timer scanning task only plays a basic timeout role, and the main timer scanning work is allocated to different tasks to complete, so as to avoid the bottleneck caused by the scanning work being concentrated on one task when the timer is used on a large scale. The threading idea is adopted to avoid the problem of software timer lock conflict, thereby improving the system throughput. It is suitable for scenarios in which the system uses a large number of software timers in a multi-core and multi-threaded concurrent environment.
[0054] In addition, a fourth aspect embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the timer task processing method described in the first aspect embodiment above.
[0055] According to the computer-readable storage medium provided by an embodiment of the present invention, the first task sets the timer to a threaded first timer queue, and the scanning work of the first timer queue is performed by the first task itself that sets the timer. The timer scanning task only plays a basic timeout role, and the main timer scanning work is allocated to different tasks to complete, so as to avoid the bottleneck caused by the scanning work being concentrated on one task when the timer is used on a large scale. The problem of software timer lock conflict is avoided by adopting the threading idea, thereby improving the system throughput. It is suitable for scenarios in which the system uses a large number of software timers in a multi-core and multi-threaded concurrent environment.
[0056] It will be appreciated by those skilled in the art that all or some of the steps and systems in the disclosed methods above may be implemented as software, firmware, hardware and appropriate combinations thereof. Some physical components or all physical components may be implemented as software executed by a processor, such as a central processing unit, a digital signal processor or a microprocessor, or implemented as hardware, or implemented as an integrated circuit, such as an application specific integrated circuit. Such software may be distributed on a computer-readable medium, which may include a computer storage medium or a non-transitory medium and a communication medium or a temporary medium. As known to those skilled in the art, the term computer storage medium includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information such as computer-readable instructions, data structures, program modules or other data. Computer storage media include, but are not limited to, RAM, ROM, EEPROM, flash memory or other memory technologies, CD-ROM, digital versatile disk DVD or other optical disk storage, magnetic cassettes, magnetic tapes, disk storage or other magnetic storage devices, or any other medium that may be used to store desired information and may be accessed by a computer. Furthermore, it is well known to those skilled in the art that communication media typically embodies computer readable instructions, data structures, program modules, or other data in a modulated data signal such as a carrier wave or other transport mechanism, and may include any information delivery media.
[0057] The embodiments of the present invention are described in detail above with reference to the accompanying drawings. However, the present invention is not limited to the above embodiments. Various changes can be made within the knowledge scope of ordinary technicians in the technical field without departing from the purpose of the present invention.
Claims
1. A method for processing a timer task, characterized in that: include: The first task is provided with a threaded first timer queue. When the first task sets a timer, the timer is set to the first timer queue, and the earliest timer in the first task is set to the timer scanning task; The second task is provided with a threaded second timer queue. When the second task sets a timer, the timer is set to the second timer queue, and the earliest timer in the second task is set to the timer scanning task; the timer scanning task is provided with a timer array, and the timer array stores the earliest timer of the first task and the earliest timer of the second task; If the timer scanning task detects that the earliest timer in the first task has expired, sending a timer expiration notification to the first task; The first task scans the first timer queue according to the expiration notification of the timer.
2. The method for processing a timer task according to claim 1, characterized in that: The timer scanning task polls and detects the earliest expiration timer of all tasks in the timer array.
3. The method for processing a timer task according to claim 1, characterized in that: The first task is also provided with a duplication checking queue for checking duplication of the timer set to the first timer queue, and the duplication checking queue adopts a red-black tree data structure.
4. The method for processing a timer task according to claim 1, characterized in that: The first timer queue adopts a red-black tree data structure, a binary tree data structure, a multi-fork tree data structure, an array data structure or a linked list data structure.
5. A software timer, characterized in that: include: A timer setting module, used to set the timer set by the first task to the threaded first timer queue of the first task, and set the earliest timer in the first task to the timer scanning task; and used to set the timer set by the second task to the threaded second timer queue of the second task, and set the earliest timer in the second task to the timer scanning task; the timer scanning task is provided with a timer array, and the timer array stores the earliest timer of the first task and the earliest timer of the second task; a timer scanning module, configured to detect whether the earliest timer in the first task has expired, and to send a timer expiration notification to the first task when the earliest timer in the first task has expired; And used to scan the first timer queue when the first task receives the timer expiration notification.
6. The software timer according to claim 5, characterized in that: The timer scanning module polls and detects the earliest expiration timer of all tasks stored in the timer array.
7. An operation control device, characterized in that: It includes at least one control processor and a memory for communicating with the at least one control processor; the memory stores instructions that can be executed by the at least one control processor, and the instructions are executed by the at least one control processor so that the at least one control processor can execute the timer task processing method as described in any one of claims 1 to 4.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the timer task processing method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Processing method and system of timers and asynchronous events
CN102455940A
Asynchronous network application program processing method
CN103092682A