Timer management method, system and device and readable storage medium
By adding smart pointers to the timer nodes, automatic cleaning of timers is achieved, memory leakage caused by the timer cleaning not being automatic and repeated cleaning in multi-threaded environments in the prior art, and system stability is improved.
Patent Information
- Application Number
- CN202411367954.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-29
- Publication Date
- 2025-01-14
AI Technical Summary
In the prior art, cleaning of timers requires manual call to clean up functions provided by programming languages or frameworks, which cannot realize automatic cleaning of timers, and it is easy to cause memory leakage and program crashes in a multi-threaded environment.
Add a smart pointer to the timer node, and automatically clean the timer using the characteristics of the smart pointer. The expiration timer is processed through the timer management thread polling, and automatically clean it when it leaves the scope to avoid repeated cleaning and memory leakage .
The automatic cleaning of timers is realized, which avoids the drawbacks of manual cleaning, prevents memory leakage and program crashes in a multi-threaded environment, and improves system stability.
Smart Images

Figure CN119311390A_ABST
Abstract
Description
Background Art
[0002] In the prior art, timers are usually manually canceled or deleted by calling a cleanup function provided by a programming language or framework. For example, in JavaScript, clearTimeout() can be used to cancel a timer set by setTimeout(), and clearInterval() can be used to cancel a timer set by setInterval(). This method requires developers to explicitly call a cleanup function when a timer is no longer needed, and cannot achieve automatic cleanup of the timer. Summary of the invention
[0003] In response to the problems raised in the background technology, the present disclosure provides a timer management method, system, device and readable storage medium. By adding a smart pointer in the timer node, the timer can be automatically cleaned up by utilizing the characteristics of the smart pointer.
[0004] Specifically, the first aspect of the present disclosure provides a timer management method, which may include the following steps:
[0005] Create a linked list for managing timers, the linked list including a timer node corresponding to each timer, and each timer node including a first smart pointer pointing to the timer node itself;
[0006] The timer management thread processes the expired timer according to the linked list polling, and resets the first smart pointer corresponding to the expired timer;
[0007] When the timer management thread leaves the scope of the first smart pointer corresponding to the expiration timer, the first smart pointer automatically cleans up the expiration timer.
[0008] In a possible implementation of the first aspect, when a service thread and a timer management thread simultaneously clean up an expired timer, the timer management method includes the following steps:
[0009] The service thread pre-creates a second smart pointer pointing to the timer node corresponding to the expiration timer;
[0010] The timer management thread resets the first smart pointer;
[0011] The business thread initiates a cleanup operation on the expiration timer and resets the first smart pointer;
[0012] When the service thread and the timer management thread leave the scope of the first smart pointer and the second smart pointer corresponding to the expiration timer, the first smart pointer and the second smart pointer automatically clean up the expiration timer.
[0013] In a possible implementation of the first aspect, during the initialization configuration of the timer node, a reference count of a first smart pointer corresponding to the timer is configured to be 1;
[0014] When the timer management thread resets the first smart pointer, the reference count of the first smart pointer corresponding to the expired timer is reduced by 1;
[0015] When the reference count of the first smart pointer corresponding to the expired timer is 0, the first smart pointer automatically cleans up the timer.
[0016] In a possible implementation of the first aspect, during the initialization configuration of the timer node, a reference count of a first smart pointer corresponding to the timer is 1;
[0017] When the service thread creates a second smart pointer, the reference count of the first smart pointer corresponding to the expiration timer increases by 1;
[0018] When the timer management thread or the service thread resets the first smart pointer, the reference count of the first smart pointer corresponding to the expired timer is reduced by 1;
[0019] When the service thread initiates a cleanup operation, the reference count of the first smart pointer corresponding to the expiration timer is reduced by 1;
[0020] When the reference count of the first smart pointer corresponding to the expired timer is 0, the first smart pointer and the second smart pointer automatically clean up the timer.
[0021] In a possible implementation of the first aspect above, when the reference count of the first smart pointer corresponding to the expiration timer is not 0, the timer management thread does not directly clean up the expiration timer.
[0022] In a possible implementation of the first aspect, when a service thread and a timer management thread simultaneously clean up an expired timer, the service thread obtains an execution lock of the expired timer, and the timer management thread synchronously obtains a timer lock of the expired timer;
[0023] When the business thread determines that the expiration timer exists, it releases the execution lock and waits for the timer lock, and creates a second smart pointer at the same time;
[0024] The timer management thread takes the execution lock and resets the first smart pointer;
[0025] The timer management thread releases the timer lock and execution lock;
[0026] The business thread takes the timer lock and initiates a cleanup operation, while resetting the first smart pointer.
[0027] In a possible implementation of the first aspect, when there are at least two service threads and a timer management thread that clean up the expired timer at the same time, each service thread creates a second smart pointer respectively;
[0028] The service thread initiates a cleanup operation on the expiration timer in sequence according to a preset order and resets the first smart pointer;
[0029] When the reference count of the smart pointer corresponding to the expiration timer is not 0, the cleanup operation initiated by the service thread does not directly clean up the expiration timer.
[0030] A second aspect of the present disclosure provides a timer management system, which is used to implement the timer management method provided in the first aspect, and may specifically include:
[0031] A timer initialization unit, used for creating a timer and for managing a linked list of timers, the linked list including a timer node corresponding to each timer, each timer node including a first smart pointer pointing to the timer node itself;
[0032] A timer cleaning unit, used for the timer management thread to poll and process the expired timer according to the linked list, and reset the first smart pointer corresponding to the expired timer to clean up the expired timer;
[0033] The pointer cleaning unit is used to automatically clean up the first smart pointer when the timer management thread leaves the scope of the first smart pointer corresponding to the expired timer.
[0034] A third aspect of the present disclosure provides a timer management device, which may specifically include: a processor;
[0035] a memory in which executable instructions for the processor are stored;
[0036] Among them, the processor is configured to execute the steps of the timer management method provided in the first aspect by executing executable instructions.
[0037] A fourth aspect of the present disclosure provides a computer-readable storage medium for storing a program, characterized in that when the program is executed by a processor, the steps of the timer management method provided in the first aspect are implemented.
[0038] Compared with the background technology, the present invention has the following beneficial effects:
[0039] The technical solution provided by the present invention, by adding a smart pointer pointing to the timer node itself, can automatically clean up the expired timer by utilizing the characteristics of the smart pointer during the process of the timer management thread polling the timer node linked list, thereby solving the drawbacks of traditional manual timer cleaning; in the process of multiple threads simultaneously cleaning up a timer, an additional smart pointer pointing to the timer is created by other business threads except the timer management thread, thereby avoiding the timer management thread and the business thread from repeatedly cleaning up the same timer, thereby causing abnormal situations such as memory leaks and program crashes. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Other features, objects and advantages of the present disclosure will become more apparent from a reading of the detailed description of non-limiting embodiments made with reference to the following accompanying drawings.
[0041] Figure 1 According to an embodiment of the present disclosure, a schematic flow chart of a timer management method is shown.
[0042] Figure 2 According to an embodiment of the present disclosure, a schematic diagram of a process of simultaneously clearing an expired timer by a service thread and a timer management thread is shown.
[0043] Figure 3 According to an embodiment of the present disclosure, a schematic diagram of image data distribution of an original image frame structure and a target image frame structure in an image synthesis process is shown.
[0044] Figure 4 According to an embodiment of the present disclosure, a structural schematic diagram of a timer management system is shown.
[0045] Figure 5 According to an embodiment of the present disclosure, a structural schematic diagram of a timer management device is provided.
[0046] Figure 6 According to an embodiment of the present disclosure, a schematic diagram of the structure of a computer-readable storage medium is provided. DETAILED DESCRIPTION
[0047] The following describes the embodiments of the present disclosure through specific examples, and those skilled in the art can easily understand other advantages and effects of the present disclosure from the contents disclosed in the present disclosure. The present disclosure can also be implemented or applied through other different specific embodiments, and the details in the present disclosure can also be modified or changed in various ways according to different viewpoints and application systems without departing from the spirit of the present disclosure. It should be noted that the embodiments in the present disclosure and the features in the embodiments can be combined with each other without conflict.
[0048] The following is a detailed description of the embodiments of the present disclosure with reference to the accompanying drawings so that those skilled in the art can easily implement the present disclosure. The present disclosure can be embodied in many different forms and is not limited to the embodiments described herein.
[0049] In the representations of the present disclosure, the reference terms "one embodiment", "some embodiments", "examples", "specific examples", or "some examples" and the like mean that the specific features, structures, materials, or characteristics represented in conjunction with the embodiment or example are included in at least one embodiment or example of the present disclosure. Moreover, the specific features, structures, materials, or characteristics represented may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine different embodiments or examples represented in the present disclosure and the features of different embodiments or examples, unless they are mutually contradictory.
[0050] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include at least one of the features. In the representation of the present disclosure, the meaning of "plurality" is two or more, unless otherwise clearly and specifically defined.
[0051] In order to clearly describe the present disclosure, components not related to the description are omitted, and the same reference numerals are given to the same or similar components throughout the specification.
[0052] Throughout the specification, when a device is said to be "connected" to another device, this includes not only the case of "direct connection" but also the case of "indirect connection" by placing other elements therebetween. In addition, when a device is said to "include" a certain component, unless otherwise stated, it does not exclude other components, but means that other components may be included.
[0053] When a device is said to be "on" another device, it may be directly on the other device, but there may also be other devices between it. In contrast, when a device is said to be "directly" on another device, there are no other devices between it.
[0054] Although the terms first, second, etc. are used to represent various elements in some examples, these elements should not be limited by these terms. These terms are only used to distinguish one element from another element. For example, the first interface and the second interface, etc. are represented. Moreover, as used in this article, the singular forms "one", "one" and "the" are intended to also include plural forms, unless there is an opposite indication in the context. It should be further understood that the terms "comprising" and "including" indicate the existence of features, steps, operations, elements, components, projects, kinds, and / or groups, but do not exclude the existence, occurrence or addition of one or more other features, steps, operations, elements, components, projects, kinds, and / or groups. The terms "or" and "and / or" used herein are interpreted as inclusive, or mean any one or any combination. Therefore, "A, B or C" or "A, B and / or C" means "any of the following: A; B; C; A and B; A and C; B and C; A, B and C". Only when the combination of elements, functions, steps or operations is inherently mutually exclusive in some way, will there be an exception to this definition.
[0055] The technical terms used herein are only used to refer to specific embodiments and are not intended to limit the present disclosure. The singular form used herein also includes the plural form unless the sentence clearly indicates the contrary meaning. The meaning of "including" used in the specification is to specify specific characteristics, regions, integers, steps, operations, elements and / or components, and does not exclude the existence or addition of other characteristics, regions, integers, steps, operations, elements and / or components.
[0056] Although not defined differently, all terms, including technical and scientific terms used herein, have the same meaning as those generally understood by those skilled in the art to which the present disclosure belongs. Terms defined in commonly used dictionaries are additionally interpreted as having meanings consistent with the content of relevant technical literature and current disclosures, and unless defined, shall not be overly interpreted as ideal or very formal meanings.
[0057] In order to overcome the problems raised in the background technology, the present disclosure provides a timer management method, system, device and readable storage medium. Specifically, in some embodiments of the present disclosure, Figure 1 FIG. 1 shows a flow chart of a timer management method. Figure 1 As shown, it may specifically include:
[0058] Step 101: Create a linked list for managing timers, the linked list includes a timer node corresponding to each timer, and each timer node includes a first smart pointer pointing to the timer node itself. The timer node is used to store relevant information of the timer, such as execution time, callback function, etc., usually including the attributes and methods of the timer, used to set the execution time of the timer, clear the timer, etc., and the linked list is used to manage and store multiple timer nodes.
[0059] Step 102: The timer management thread polls the expired timer according to the linked list and resets the first smart pointer corresponding to the expired timer to clear the expired timer. The specific polling process of the timer management thread will be described in detail later.
[0060] Step 103: When the timer management thread leaves the scope of the first smart pointer corresponding to the expired timer, the first smart pointer is automatically cleaned up. It can be understood that based on the characteristics of the smart pointer, when the timer management thread leaves the scope of the first smart pointer corresponding to the expired timer, the first smart pointer itself will also be automatically cleaned up to release memory.
[0061] In the above embodiment, during the initialization configuration of the timer node, the reference count of the first smart pointer corresponding to the timer is configured to be 1; when the timer management thread resets the first smart pointer, the reference count of the first smart pointer corresponding to the expiration timer is reduced by 1. Under normal circumstances, if there are no other business threads that need to clean up the expiration timer, after the timer management thread resets the first smart pointer of the expiration timer, the reference count of the first smart pointer corresponding to the expiration timer changes from 1 to 0, that is, when the reference count of the smart pointer corresponding to the expiration timer is 0, the characteristics of the smart pointer can be used to automatically clean up the timer.
[0062] It is understandable that, for a smart pointer, as a class, a counter is associated with the object pointed to by the class, i.e., the timer node provided in the present disclosure, and a reference count is used to track how many objects of the class share the same pointer: each time a new object of the smart pointer class is created, the pointer is initialized and the reference count is set to 1, and if the reference count is reduced to 0, the object is deleted, so that the timer can be automatically cleaned up. It is understandable that by adding the above-mentioned smart pointer structure to the timer linked list, the smart pointer reference count corresponding to the expired timer can be adjusted in the process of the timer management thread polling the timer, thereby utilizing the characteristics of the smart pointer to automatically clean up the expired timer, instead of manually and explicitly calling the cleanup function to clean up the timer after the timer expires in the prior art, and more efficiently and conveniently realizing the automatic cleaning and maintenance of the timer.
[0063] In some embodiments of the present disclosure, the addition of a smart pointer structure can also prevent multiple threads from cleaning up a timer at the same time, thereby causing repeated timer cleaning, memory leaks, and program crashes. It is understandable that when other service threads other than the timer management thread need to clean up a timer, it will be pre-determined whether the timer exists: if it does not exist, it means that the timer has been cleaned up by the timer management thread or other service threads, and the cleaning action will not be initiated; if it exists, the service thread will perform a cleaning operation on the timer.
[0064] In the scenario where multiple threads clean up a timer at the same time, for example, a business thread and a timer management thread clean up a timer at the same time. In order to prevent deadlock, the locks of the two threads cannot be taken at the same time. At this time, the business thread determines that there is a timer that needs to be cleaned, releases the execution lock and waits for the timer lock; the timer management thread polls the timer according to the linked list, takes the execution lock and cleans the timer, and then releases the timer lock and the execution lock; after the business thread completes the wait, it needs to take the lock to clean up the timer, but at this time the timer has been cleaned up by the timer management thread, and repeated cleaning of the business thread will cause a crash. It can be understood that the above-mentioned simultaneous cleaning can refer to the scenario where multiple threads initiate a cleaning operation on the same timer at the same time, or it can refer to the scenario where other threads initiate a cleaning operation on the timer while one thread is performing a cleaning operation on the timer, which is not limited here.
[0065] To avoid duplicate cleaning and crashes when multiple threads clean up timers at the same time, Figure 2 According to an embodiment of the present disclosure, a flow chart of a process in which a service thread and a timer management thread simultaneously clean up an expired timer is shown. Figure 2 As shown, the following steps may be specifically included:
[0066] Step 201: The business thread pre-creates a second smart pointer pointing to the timer node corresponding to the expiration timer. It is understandable that when the business thread creates the second smart pointer, the reference count of the first smart pointer corresponding to the expiration timer increases by 1, which can avoid the timer management thread from directly completing the cleanup of the expiration timer after resetting the first smart pointer. It is understandable that based on the characteristics of smart pointers, since the first smart pointer and the second smart pointer point to the same timer node, their corresponding reference counts are the same. For ease of explanation, the following will be uniformly expressed in terms of the reference count of the first smart pointer.
[0067] Step 202: The timer management thread resets the first smart pointer. It is understandable that at this time, since the service thread has created a second smart pointer, the expiration timer still has a new pointer, and the reference count of the first smart pointer corresponding to the expiration timer is not 0, and will not be automatically cleared by the timer management thread.
[0068] Step 203: The service thread initiates a cleanup operation on the expired timer and resets the first smart pointer. It is understandable that at this time, the service thread can normally perform the cleanup work on the expired timer without crashing.
[0069] Step 204: When the service thread and the timer management thread leave the scope of the first smart pointer and the second smart pointer corresponding to the expired timer, the first smart pointer and the second smart pointer are automatically cleaned up. It can be understood that based on the characteristics of the smart pointer, when the service thread and the timer management thread leave the scope of the first smart pointer and the second smart pointer corresponding to the expired timer, the first smart pointer and the second smart pointer themselves will also be automatically cleaned up to release memory.
[0070] In the above embodiment, it can be understood that, when the service thread creates the second smart pointer, the reference count of the first smart pointer corresponding to the expiration timer increases by 1; when the timer management thread or the service thread resets the first smart pointer, the reference count of the first smart pointer corresponding to the expiration timer decreases by 1; when the service thread initiates a cleanup operation, the reference count of the first smart pointer corresponding to the expiration timer decreases by 1; when the reference count of the first smart pointer corresponding to the expiration timer is 0, the first smart pointer and the second smart pointer automatically clean up the timer. In other words, when the reference count of the first smart pointer corresponding to the expiration timer is not 0, the timer management thread does not directly clean up the expiration timer.
[0071] In the above embodiment, further, Figure 3 The schematic diagram of the timer cleanup process of the business thread and the timer management thread is shown. Figure 3 As shown, it can be seen that the following steps are included:
[0072] Step 301: The service thread obtains the execution lock of the expired timer, and the timer management thread synchronously obtains the timer lock of the expired timer. It can be understood that since the service thread and the timer management thread both process the same timer, and to prevent deadlock, they cannot obtain the locks of both threads at the same time, so the service thread needs to release the execution lock in the subsequent steps.
[0073] Step 302: When the business thread determines that an expiration timer exists, it releases the execution lock and waits for the timer lock, and creates a second smart pointer at the same time.
[0074] Step 303: The timer management thread takes the execution lock and resets the first smart pointer.
[0075] Step 304: The timer management thread releases the timer lock and the execution lock.
[0076] Step 305: The business thread takes the timer lock and initiates a cleanup operation, while resetting the first smart pointer.
[0077] In other embodiments of the present disclosure, for scenarios where at least two service threads and a timer management thread simultaneously clean up expired timers, the timer management method provided by the present disclosure can also be used to avoid repeated cleaning of the same timer. Specifically, each service thread that needs to perform timer cleaning at the same time can independently determine whether there is a timer that needs to be cleaned up, and if so, each service thread independently creates a second smart pointer pointing to the expired timer.
[0078] In the above embodiment, during the specific cleaning execution process of the expiration timer, the timer management thread also polls the linked list and executes the expiration timer, and resets the first smart pointer. At this time, since multiple business threads still have new pointers, the reference count of the first smart pointer corresponding to the expiration timer is not 0, and the expiration timer is not automatically cleaned up; then at least two business threads can execute the lock-taking operation on the timer lock in sequence according to the preset order and initiate the cleaning operation, and reset the first smart pointer at the same time. It can be understood that in the case where there are other business threads that need to perform expiration timer cleaning, since there are still new pointers, the reference count of the first smart pointer corresponding to the expiration timer is still not 0, and the expiration timer will not be directly cleaned up by the current business thread lock; that is, only the last business thread to perform the cleaning operation in the preset order can clean up the expiration timer, and the same timer will not be cleaned up repeatedly.
[0079] In some embodiments of the present disclosure, Figure 4 A schematic diagram of the structure of a timer management system is provided. Figure 4 As shown, this timer management system is used to implement the timer management method provided in the above embodiment, which may specifically include:
[0080] A timer initialization unit 401, used to create a timer and a linked list for managing the timer, the linked list including a timer node corresponding to each timer, each timer node including a first smart pointer pointing to the timer node itself;
[0081] The timer cleaning unit 402 is used to control the timer management thread to process the expired timer according to the linked list polling, and reset the first smart pointer corresponding to the expired timer to clean up the expired timer;
[0082] The pointer cleaning unit 403 is used to automatically clean up the first smart pointer when the timer management thread leaves the scope of the first smart pointer corresponding to the expired timer.
[0083] It is understandable that the functions implemented by the timer initialization unit 401 to the pointer cleaning unit 403 in the above functional modules correspond one-to-one to the actions performed by the above steps 101 to 103, and are not described in detail here.
[0084] Those skilled in the art will appreciate that various aspects of the present disclosure may be implemented as systems, methods or program products. Therefore, various aspects of the present disclosure may be specifically implemented in the following forms, namely: complete hardware implementation, complete software implementation (including firmware, microcode, etc.), or a combination of hardware and software implementations, which may be collectively referred to herein as "circuits", "modules" or "platforms".
[0085] Specifically, Figure 5 According to an embodiment of the present disclosure, a schematic diagram of the structure of a timer management device is shown. Figure 5 The electronic device 600 according to this embodiment of the present disclosure is described. Figure 5 The electronic device 600 shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0086] like Figure 5 As shown, the electronic device 600 is in the form of a general computing device. The components of the electronic device 600 may include, but are not limited to: at least one processing unit 610, at least one storage unit 620, a bus 630 connecting different platform components (including the storage unit 620 and the processing unit 610), a display unit 640, etc.
[0087] The storage unit stores program codes, which can be executed by the processing unit 610, so that the processing unit 610 performs the steps according to various exemplary embodiments of the present disclosure. For example, the processing unit 610 can perform the following steps: Figure 1 The relevant steps of the timer management method shown in .
[0088] The storage unit 620 may include a readable medium in the form of a volatile storage unit, such as a random access memory (RAM) 6201 and / or a cache storage unit 6202 , and may further include a read-only storage unit (ROM) 6203 .
[0089] The storage unit 620 may also include a program / utility 6204 having a set (at least one) of program modules 6205, such program modules 6205 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0090] Bus 630 may represent one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
[0091] The electronic device 600 may also communicate with one or more external devices 700 (e.g., keyboards, pointing devices, Bluetooth devices, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 600, and / or communicate with any device that enables the electronic device 600 to communicate with one or more other computing devices (e.g., routers, modems, etc.). Such communication may be performed via an input / output (I / O) interface 650. Furthermore, the electronic device 600 may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via a network adapter 660. The network adapter 660 may communicate with other modules of the electronic device 600 via a bus 630. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 600, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage platforms.
[0092] The embodiment of the present disclosure also provides a computer-readable storage medium for storing a program, and the steps of the timer management method are implemented when the program is executed. In some possible implementations, various aspects of the present disclosure can also be implemented in the form of a program product, which includes a program code. When the program product is run on a terminal device, the program code is used to enable the terminal device to execute the steps according to various exemplary implementations of the present disclosure described in the above timer management method section of this specification.
[0093] Specifically, Figure 6 According to an embodiment of the present disclosure, a schematic diagram of the structure of a computer-readable storage medium is shown. Figure 6As shown, a program product 800 for implementing the above-mentioned timer management method according to an embodiment of the present disclosure is described, which can adopt a portable compact disk read-only memory (CD-ROM) and include program code, and can be run on a terminal device, such as a personal computer. However, the program product of the present disclosure is not limited thereto. In this document, a readable storage medium can be any tangible medium containing or storing a program, which can be used by or in combination with an instruction execution system, an apparatus or a device.
[0094] The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination thereof. More specific examples (a non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.
[0095] Computer readable storage media may include data signals propagated in baseband or as part of a carrier wave, in which readable program codes are carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The readable storage medium may also be any readable medium other than a readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, device, or device. The program code contained on the readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination of the above.
[0096] The program code for executing the specific implementation operations of the timer management method provided by the aforementioned embodiments of the present disclosure can be written in any combination of one or more programming languages, including object-oriented programming languages such as Java, C++, etc., and also conventional procedural programming languages such as "C" language or similar programming languages. The program code can be executed entirely on the user computing device, partially on the user device, as an independent software package, partially on the user computing device and partially on the remote computing device, or completely on the remote computing device or server. In the case of a remote computing device, the remote computing device can be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computing device (for example, using an Internet service provider to connect through the Internet).
[0097] To summarize, through the technical solution provided by the present invention, by adding a smart pointer pointing to the timer node itself, the expired timer can be automatically cleaned up by utilizing the characteristics of the smart pointer during the process of the timer management thread polling the timer node linked list, thereby solving the drawbacks of traditional manual timer cleaning. In the process of multiple threads cleaning up a timer at the same time, an additional smart pointer pointing to the timer is created through other business threads except the timer management thread, thereby avoiding the timer management thread and the business thread from repeatedly cleaning up the same timer, thereby causing abnormal situations such as memory leaks and program crashes.
[0098] The above content is a further detailed description of the present disclosure in combination with specific preferred implementation modes, and it cannot be determined that the specific implementation of the present disclosure is limited to these descriptions. For ordinary technicians in the technical field to which the present disclosure belongs, several simple deductions or substitutions can be made without departing from the concept of the present disclosure, which should be regarded as falling within the scope of protection of the present disclosure.
Claims
1. A timer management method, characterized in that: The steps include: Creating a linked list for managing timers, the linked list comprising a timer node corresponding to each of the timers, each of the timer nodes comprising a first smart pointer pointing to the timer node itself; The timer management thread polls the expiration timer according to the linked list, and resets the first smart pointer corresponding to the expiration timer to clean up the expiration timer; When the timer management thread leaves the scope of the first smart pointer corresponding to the expiration timer, the first smart pointer is automatically cleaned up.
2. The timer management method according to claim 1, characterized in that: In the case where a service thread and the timer management thread simultaneously clean up the expired timer, the timer management method comprises the following steps: The service thread pre-creates a second smart pointer pointing to the timer node corresponding to the expiration timer; The timer management thread resets the first smart pointer; The service thread initiates a cleanup operation on the expiration timer and resets the first smart pointer; When the service thread and the timer management thread leave the scope of the first smart pointer and the second smart pointer corresponding to the expiration timer, the first smart pointer and the second smart pointer are automatically cleaned up.
3. The timer management method according to claim 1, characterized in that: During the initialization configuration of the timer node, the reference count of the smart pointer corresponding to the timer is configured to 1; When the timer management thread resets the first smart pointer, the reference count of the first smart pointer corresponding to the expiration timer is reduced by 1; When the reference count of the first smart pointer corresponding to the expired timer is 0, the timer is automatically cleared.
4. The timer management method according to claim 2, characterized in that: During the initialization configuration of the timer node, the reference count of the smart pointer corresponding to the timer is 1; When the service thread creates the second smart pointer, the reference count of the first smart pointer corresponding to the expiration timer is increased by 1; When the timer management thread or the service thread resets the first smart pointer, the reference count of the first smart pointer corresponding to the expiration timer is reduced by 1; When the service thread initiates the cleanup operation, the reference count of the first smart pointer corresponding to the expiration timer is reduced by 1; When the reference count of the first smart pointer corresponding to the expired timer is 0, the timer is automatically cleared.
5. The timer management method according to claim 4, characterized in that: When the reference count of the first smart pointer corresponding to the expiration timer is not 0, the timer management thread does not directly clean up the expiration timer.
6. The timer management method according to claim 2, characterized in that: In the case where the service thread and the timer management thread simultaneously clean up the expired timer, the service thread takes the execution lock of the expired timer, and the timer management thread synchronously takes the timer lock of the expired timer; When the service thread determines that the expiration timer exists, the service thread releases the execution lock and waits for the timer lock, and creates the second smart pointer at the same time; The timer management thread takes the execution lock and resets the first smart pointer; The timer management thread releases the timer lock and the execution lock; The service thread takes the timer lock and initiates the cleanup operation, while resetting the first smart pointer.
7. The timer management method according to claim 2, characterized in that: In the case that at least two of the service threads and the timer management thread simultaneously clean up the expired timer, each of the service threads creates the second smart pointer respectively; The service thread initiates the cleanup operation on the expiration timer in sequence according to a preset order, and resets the first smart pointer; When the reference count of the smart pointer corresponding to the expiration timer is not 0, the cleanup operation initiated by the service thread does not directly clean up the expiration timer.
8. A timer management system, used to implement the timer management method according to any one of claims 1 to 7, characterized in that: include: A timer initialization unit, used for creating a timer and for managing a linked list of the timer, wherein the linked list includes a timer node corresponding to each of the timers, and each of the timer nodes includes a first smart pointer pointing to the timer node itself; A timer cleaning unit, used for the timer management thread to poll and process the expired timer according to the linked list, and reset the first smart pointer corresponding to the expired timer to clean up the expired timer; A pointer cleaning unit is used to automatically clean up the first smart pointer when the timer management thread leaves the scope of the first smart pointer corresponding to the expiration timer.
9. A timer management device, characterized in that: include: processor; a memory storing executable instructions of the processor; The processor is configured to execute the steps of the timer management method according to any one of claims 1 to 7 by executing the executable instructions.
10. A computer-readable storage medium for storing a program, characterized in that: When the program is executed by a processor, the steps of the timer management method according to any one of claims 1 to 7 are implemented.