Thread resource release method and device, equipment, medium and program product

By automatically releasing thread resources by using the cleaning function in the TLS space when a thread exits, the problem of resource leakage in the existing technology is solved and system stability and development efficiency are improved.

CN119781995AActive Publication Date: 2025-04-08ZHEJIANG GEELY HLDG GRP CO LTD +1
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202510286633.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-12
Publication Date
2025-04-08
Estimated Expiration
2045-03-12

AI Technical Summary

Technical Problem

In the prior art, thread resource release methods require staff to write code instructions or functions for each thread, which are prone to writing errors or forgetting to write, resulting in resource leakage.

Method used

When a thread exits, the thread resource is automatically released using the cleaning function in the thread-local storage (TLS) space. It is divided into three types of resources: resources that can only be called by threads, resources that can only be used by threads, and resources that can be used by all threads in the process.

Benefits of technology

It realizes the automatic release of thread resources, avoids resource leakage, and improves system stability and development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119781995A_ABST
    Figure CN119781995A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a thread resource release method and device, equipment, a medium and a program product. In the method, when a thread exits, for each resource which can be only used by a thread function called by the thread in a local storage space of the thread, the resource is released by the thread according to a cleaning function corresponding to the resource in the local storage space of the thread; for each resource which can only be used by the thread in the local storage space of the thread, releasing the resource according to a cleaning function corresponding to the resource in the local storage space; and when the process exits, the operating system releases resources which can be used by all the threads in the process in the heap space. According to the scheme, when the thread exits and exits, the three resources of the thread are released, the situation of resource leakage is avoided, and the system stability is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a thread resource release method, device, equipment, medium and program product. Background Art

[0002] In a computer operating system, various programs are run as a process, and in each process, multiple threads are running. Before and during the running process, threads can apply for resources to realize the functions of threads. Resources can be address spaces, file descriptors, semaphores, etc. In order to avoid resource leakage, thread resources need to be released when the thread exits.

[0003] In the prior art, thread exit is divided into normal exit and exit by receiving a cancel instruction. When a thread exits normally, it will execute the code instructions written by the staff for releasing resources to release thread resources, such as free, close, etc. When a thread receives an exit instruction, it will execute the function written by the staff for releasing resources to release thread resources, such as pthread_cleanup_push and pthread_cleanup_pop, etc.

[0004] In summary, the existing thread resource release method requires staff to write code instructions or functions for each thread, which is prone to writing errors or forgetting to write, resulting in resource leakage. Summary of the invention

[0005] The thread resource release method, apparatus, device, medium and program product provided in the embodiments of the present application are used to solve the resource leakage problem that occurs in the existing thread resource release method.

[0006] In a first aspect, an embodiment of the present application provides a thread resource release method, comprising:

[0007] When a thread exits, the thread releases each first thread resource in the thread local storage TLS space according to a cleanup function corresponding to the first thread resource stored in the TLS space, where the first thread resource is a resource that can only be used by a thread function called by the thread;

[0008] When the thread exits, the thread releases each second thread resource in the TLS space according to a cleanup function corresponding to the second thread resource stored in the TLS space, where the second thread resource is a resource that can only be used by the thread;

[0009] When the process to which the thread belongs exits, the operating system releases the third thread resource in the heap space of the process, where the third thread resource is a resource that can be used by all threads in the process.

[0010] In a possible implementation manner, the first thread resource and a cleanup function corresponding to the first thread resource are stored in a first structure in the TLS space;

[0011] The second thread resource and the cleanup function corresponding to the second thread resource are stored in a second structure in the TLS space.

[0012] In a possible implementation, the method further includes:

[0013] When the thread function called by the thread ends execution, the thread executes a release instruction, and the release instruction is used to release the first thread resources in each first structure in the function stack space of the thread function according to the cleanup function in the first structure, and delete the first structure from the TLS space.

[0014] In a possible implementation, when the function called by the thread ends execution and before the thread executes a release instruction, the method further includes:

[0015] When compiling the source code of the process, the compiler inserts the release instruction after each thread function called by the thread.

[0016] In a possible implementation, the method further includes:

[0017] Before applying for resources, the thread shields a cancel instruction, wherein the cancel instruction is used to exit the thread;

[0018] After applying for resources, the thread performs the following processing:

[0019] Determine the storage location and storage method of the resource according to the type of the resource;

[0020] Storing the resource in the storage location according to the storage method;

[0021] Unblock cancels the command.

[0022] In a possible implementation manner, determining the storage location and storage method of the resource according to the type of the resource includes:

[0023] If the resource is a first thread resource, determining the storage location to be the TLS space and a function stack space of a thread function using the resource, or determining the storage location to be the TLS space;

[0024] The storage method is determined as: combining the resource with a cleanup function to generate a first structure, and storing the first structure in a storage location.

[0025] In a possible implementation, the method further includes:

[0026] If the resource is a second thread resource, determining the storage location to be the TLS space;

[0027] The storage method is determined as: combining the resource with a cleanup function to generate a second structure, and storing the second structure in a storage location.

[0028] In a possible implementation, the method further includes:

[0029] If the resource is a third thread resource, determining the storage location to be a heap space of the process;

[0030] Determining the storage method is: storing the resource in the storage location; or,

[0031] The resource and the cleanup function are combined to generate a second structure, the second structure is stored in the TLS space, the resource is stored in the storage location, and the second structure is deleted.

[0032] In a second aspect, an embodiment of the present application provides a thread resource release device, including:

[0033] The first resource release module is used to:

[0034] When a thread exits, for each first thread resource in the thread local storage TLS space, the first thread resource is released according to a cleanup function corresponding to the first thread resource stored in the TLS space by the thread, where the first thread resource is a resource that can only be used by a thread function called by the thread;

[0035] When the thread exits, for each second thread resource in the TLS space, the second thread resource is released by the thread according to a cleanup function corresponding to the second thread resource stored in the TLS space, where the second thread resource is a resource that can only be used by the thread;

[0036] The second resource release module is used to release the third thread resource in the heap space of the process through the operating system when the process to which the thread belongs exits, and the third thread resource is a resource that can be used by all threads in the process.

[0037] In a third aspect, an embodiment of the present application provides an electronic device, including:

[0038] Processor, memory, communication interface;

[0039] The memory is used to store executable instructions of the processor;

[0040] The processor is configured to execute the thread resource releasing method described in any one of the first aspects by executing the executable instructions.

[0041] In a fourth aspect, an embodiment of the present application provides a readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the thread resource release method described in any one of the first aspects.

[0042] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, is used to implement the thread resource release method described in any one of the first aspects.

[0043] The thread resource release method, device, equipment, medium and program product provided by the embodiment of the present application releases the resource in the thread local storage space for each resource that can only be used by the thread function called by the thread when the thread exits, according to the cleanup function corresponding to the resource in the thread local storage space; and for each resource in the thread local storage space that can only be used by the thread, according to the cleanup function corresponding to the resource in the local storage space; when the process exits, the operating system releases the resources in the heap space that can be used by all threads in the process. This solution releases the three resources of the thread when the thread exits and exits, and does not cause resource leakage, thereby improving system stability. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0045] Figure 1a A flowchart of a first embodiment of a thread resource release method provided in the present application;

[0046] Figure 1b A schematic diagram of the use of the third thread resource provided for this application;

[0047] Figure 1c A schematic diagram of the process of writing thread code in the prior art provided by this application;

[0048] Figure 1d A flowchart of the thread code writing process provided for this application;

[0049] Figure 2aA flowchart of a third embodiment of the thread resource release method provided in this application;

[0050] Figure 2b A flowchart of thread management resources provided for this application;

[0051] Figure 3 A structural diagram of an embodiment of a thread resource release device provided by the present application;

[0052] Figure 4 A schematic diagram of the structure of an electronic device provided in this application.

[0053] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION

[0054] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0055] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0056] In a computer operating system, various programs are run as a process, and in each process, multiple threads are running. Before and during the running process, threads can apply for resources to realize the functions of threads. Resources can be address spaces, file descriptors, semaphores, thread locks, etc. In order to avoid resource leakage, thread resources need to be released when the thread exits.

[0057] Since the same cleanup procedure is executed when each thread exits to clean up some thread-related information, which does not include the resources requested by the thread, staff are required to write corresponding code instructions or resource release functions for each thread to release thread resources.

[0058] The thread exit is divided into normal exit and exit by receiving a cancel instruction. When the thread exits normally, the code instructions written by the staff for releasing resources will be executed to release thread resources, such as free, close, etc. When the thread receives an exit instruction, the cancel instruction can be pthread_cancel, and the function written by the staff for releasing resources will be executed to release thread resources, such as pthread_cleanup_push and pthread_cleanup_pop.

[0059] Since staff are required to write code instructions or functions for each thread, it is easy to make mistakes or forget to write, which will cause resources to be unable to be released, thus causing resource leakage.

[0060] In view of the problems existing in the prior art, the inventors found in the process of studying the thread resource release method that in order to reduce the occurrence of resource leakage, the staff can be prevented from writing corresponding code instructions or functions for each thread, and the thread resources can be automatically released when the thread exits and the process exits. Thread resources are divided into three types. The first type is a resource that can only be used by a thread function called by a thread, which can be combined with a cleanup function to form a first structure and stored in the thread's thread local storage (Thread Local Storage, abbreviated as: TLS) space. The second type is a resource that can only be used by a thread, which can be combined with a cleanup function to form a second structure and stored in the TLS space. The third type is a resource that can be used by all threads in the process, which can be stored in the heap space of the process. The TLS space of a thread can only be accessed by the thread, and each thread of the process can access the heap space of the process.

[0061] The staff can insert a release program in the cleanup program so that the release program will be executed when the thread exits, that is, when the thread exits, the thread releases the resources in the first structure according to the cleanup function in the first structure; and releases the resources in the second structure according to the cleanup function in the second structure. When the process exits, the operating system releases the resources in the heap space. Based on the above inventive concept, the thread resource release scheme in this application is designed.

[0062] The following is an example of an application scenario of the thread resource release method provided in this application.

[0063] Exemplarily, in this application scenario, a first structure and a second structure are stored in the TLS space of the thread, the first structure includes a first thread resource and a cleanup function, and the first thread resource is a resource that can only be used by the thread function called by the thread. The second structure includes a second thread resource and a cleanup function, and the second thread resource is a resource that can only be used by the thread. A third thread resource is stored in the heap space of the process to which the thread belongs, and the third thread resource is a resource that can be used by all threads in the process. There is a corresponding relationship between the first thread resource in the first structure and the cleanup function, and there is a corresponding relationship between the second thread resource in the second structure and the cleanup function.

[0064] Since the staff inserts the release program into the cleanup program, the release program will be executed during the execution of the cleanup program when the thread exits. That is, when the thread exits, for each first structure in the TLS space, the thread releases the first thread resources in the first structure according to the cleanup function in the first structure; and for the second structure in the TLS space, the thread releases the second thread resources in the second structure according to the cleanup function in the second structure.

[0065] In addition, since the operating system releases all resources in the heap space of the process when the process exits, and since the third thread resources of the thread are stored in the heap space, the operating system releases the third thread resources when the process exits.

[0066] It should be noted that the above scenario is only a schematic diagram of an application scenario provided by an embodiment of the present application. The embodiment of the present application does not limit the devices running processes, threads and operating systems in the scenario, which can be computers, servers, terminal devices, etc. In the specific application of the solution, it can be set according to actual needs.

[0067] The technical solution of the present application is described in detail below through specific embodiments. It should be noted that the following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments.

[0068] Figure 1a This is a flowchart of the first embodiment of the thread resource release method provided by the present application. The present embodiment of the present application describes the situation in which a thread, when exiting, releases resources of thread functions that can only be called by it, and resources that can only be used by itself through a cleanup function; when a process exits, the operating system releases resources that can be used by all threads in the process. The method in this embodiment can be implemented by software, hardware, or a combination of software and hardware. Figure 1a As shown, the thread resource release method specifically includes the following steps:

[0069] S101: When a thread exits, the thread releases each first thread resource in the TLS space according to a cleanup function corresponding to the first thread resource stored in the TLS space.

[0070] There are three types of thread resources. The first type is a resource that can only be used by a thread function called by a thread. It can be combined with its corresponding cleanup function to form a first structure stored in the thread's TLS space, called the first thread resource. The first thread resource and its corresponding cleanup function are stored in the first structure in the TLS space.

[0071] The second type is a resource that can only be used by a thread, which can be combined with its corresponding cleanup function to form a second structure stored in the TLS space, called a second thread resource. The second thread resource and its corresponding cleanup function are stored in the second structure in the TLS space.

[0072] The third type is a resource that can be used by all threads in the process and can be stored in the process's heap space. It is called the third thread resource. The TLS space of a thread can only be accessed by the thread, and each thread of the process can access the process's heap space.

[0073] For example, Figure 1b This is a schematic diagram of the use of the third thread resource provided by this application. Figure 1b As shown, the process includes three threads, namely thread 1, thread 2 and thread 3. After thread 2 applies for the third thread resource, it stores it in the heap space. Thread 1 and thread 3 can access the heap space and obtain the third thread resource from the heap space.

[0074] In this step, since the staff inserts the release program into the cleanup program, the release program will be executed during the execution of the cleanup program when the thread exits. That is, when the thread exits, the thread releases each first thread resource in the TLS space according to the cleanup function corresponding to the first thread resource stored in the TLS space.

[0075] In other words, when the thread exits, the thread releases the first thread resources in each first structure in the TLS space according to the cleanup function in the first structure.

[0076] It should be noted that the cleanup function is a function that releases thread resources, which may be cleanup, close, free, etc. The embodiment of the present application does not limit the cleanup function and can be determined according to actual conditions.

[0077] Exemplarily, the format of the first structure may be:

[0078] typedef struct thread_resource {

[0079] struct thread_resource* next;

[0080] void (*cleanup)(void*);

[0081] void* val;

[0082] }thread_resource_t;

[0083] Among them, val represents the first thread resource, and cleanup represents the cleanup function.

[0084] The embodiment of the present application does not limit the style of the first structure, which can be determined according to actual conditions.

[0085] S102: When the thread exits, the thread releases each second thread resource in the TLS space according to a cleanup function corresponding to the second thread resource stored in the TLS space.

[0086] In this step, a release program is executed during the execution of a cleanup program when the thread exits, that is, the thread releases each second thread resource in the TLS space according to a cleanup function corresponding to the second thread resource stored in the TLS space.

[0087] In other words, for each second structure in the TLS space, the thread releases the second thread resources in the second structure according to the cleanup function in the second structure.

[0088] It should be noted that the second structure is similar to the first structure, and will not be further described here.

[0089] It should be noted that the cleanup function in the second structure may be the same as or different from the cleanup function in the first structure.

[0090] It should be noted that the execution order of step S101 and step S102 may be: step S101 is executed first, then step S102; or: step S102 is executed first, then step S101; or: step S101 and step S102 are executed simultaneously. The implementation of this application does not limit the execution order of step S101 and step S102, which can be determined according to actual conditions.

[0091] S103: When the process to which the thread belongs exits, the operating system releases the third thread resources in the heap space of the process.

[0092] In this step, since the third thread resources of the process are stored in the heap space of the process, when the process exits, the operating system will release all resources in the heap space of the process, so the operating system will release the third thread resources.

[0093] The thread resource release method provided in this embodiment is that when the thread exits, the thread releases the resources in the first structure that can only be used by the thread function called by the thread according to the cleanup function in the first structure in the thread local storage space; and according to the cleanup function in the second structure in the local storage space, the resources that can only be used by the thread are released; when the process exits, the operating system releases the resources in the heap space that can be used by all threads in the process. Compared with the prior art that requires staff to write code instructions or functions for each thread, this solution releases the three resources of the thread when the thread exits and exits, so that resources will not be occupied all the time and resource leakage will not occur, thereby improving system stability, system performance and system security.

[0094] In addition, this solution does not require staff to write code instructions or functions for each thread, which can improve development efficiency and code maintainability. This solution can release all thread resources, including releasing thread locks, which can improve system stability.

[0095] For example, Figure 1c The flowchart of writing thread code in the prior art provided by this application is as follows: Figure 1c As shown, after the staff has finished writing the thread function code, they need to write the code instructions for releasing resources. Figure 1d The flowchart of writing thread code provided for this application is as follows: Figure 1d As shown, the staff only needs to write the thread function code, and does not need to write code instructions for each thread, which can improve development efficiency and code maintainability.

[0096] On the basis of the above embodiments, the second embodiment of the thread resource release method provided by the present application is used below to illustrate the situation where a thread releases a first thread resource when a thread function execution ends.

[0097] Since the first thread resource is a resource used only by the thread function, in order to improve resource utilization, the first thread resource may be released after the thread function is executed.

[0098] In addition to being stored in the TLS space, the first structure is also stored in the function stack space of the thread function. The first structures stored in the two spaces are the same.

[0099] There is a corresponding insertion instruction code in the source code of the process. When the compiler compiles the source code of the process, it inserts a release instruction after each thread function called by the thread. The release instruction is used for each first structure in the function stack space of the thread function. According to the cleanup function in the first structure, the first thread resource in the first structure is released, and the first structure is deleted from the TLS space.

[0100] Therefore, when the thread function called by the thread ends execution, the thread executes a release instruction to release the first thread resources, and in order to avoid repeated release, the first structure is deleted from the TLS space.

[0101] In addition, when the thread receives a cancel instruction and exits, since the thread function has not been executed yet, the first thread resources will not be released when the thread function ends. At this time, the thread will release the first thread resources according to the first structure in the TLS space, which will not cause resource leakage.

[0102] Exemplarily, the style of the inserted instruction code may be:

[0103] #define LOCAL_FD(VAL) \ __attribute__((__cleanup__(__close_fd))) intVAL;

[0104] #define LOCAL_MEM(VAL)\ __attribute__((__cleanup__(__free_mem))) intVAL;

[0105] The embodiment of the present application does not limit the style of the inserted instruction code, which can be determined according to actual conditions.

[0106] The thread resource release method provided in this embodiment releases the first thread resource when the thread function ends, so that the resource release is more timely, the resource occupation time is reduced, and the resource utilization rate is improved.

[0107] Figure 2a This is a flow chart of the third embodiment of the thread resource release method provided by this application. Based on the above embodiment, this embodiment of the application describes the situation of thread resource application. Figure 2a As shown, the thread resource release method specifically includes the following steps:

[0108] S201: Shield cancel instruction.

[0109] In this step, if the thread is applying for resources when receiving the cancel instruction, the thread will not be able to release the applied resources, causing resource leakage. Therefore, the thread needs to block the cancel instruction before applying for resources. The cancel instruction is used to exit the thread.

[0110] Exemplarily, the function used by the thread to implement the shield cancellation instruction may be SIG_BLOCK signoSIGCANCEL.

[0111] S202: Apply for resources.

[0112] In this step, the thread will no longer execute the cancel instruction during the period of shielding the cancel instruction, so the resource application during this period will not cause resource leakage. After the thread shields the cancel instruction, the thread can apply for resources.

[0113] It should be noted that the function used to apply for resources can be malloc, realioc, calloc, etc. The embodiment of the present application does not limit the function used to apply for resources, and it can be determined according to actual conditions.

[0114] S203: Determine the storage location and storage method of the resource according to the type of the resource.

[0115] In this step, after the thread applies for resources, in order to release the resources later, it is necessary to determine the storage location and storage method of the resources according to the type of resources.

[0116] When a thread applies for a resource, it can determine the permission to use the resource, and thus the type of the resource. When the permission to use the resource is for thread function use, the type of the resource is first thread resource; when the permission to use the resource is for thread use alone, the type of the resource is second thread resource; when the permission to use the resource is for multi-thread sharing, the type of the resource is third thread resource.

[0117] Specifically, if the resource is a first thread resource, the storage location is determined to be the TLS space and the function stack space of the thread function using the resource, or the storage location is determined to be the TLS space. The storage method is determined to be: combining the resource with the cleanup function to generate a first structure, and storing the first structure to the storage location.

[0118] It should be noted that the resource and the cleanup function are combined to generate the first structure, indicating that there is a corresponding relationship between the resource and the cleanup function.

[0119] Since the first thread resource is a resource that can only be used by a thread function called by a thread, it needs to be stored in the TLS space. In order to release the first thread resource in time, it can also be stored in the function stack space.

[0120] If the resource is a second thread resource, the storage location is determined to be the TLS space. The storage method is determined as: combining the resource with the cleanup function to generate a second structure, and storing the second structure to the storage location.

[0121] It should be noted that the resource and the cleanup function are combined to generate the second structure, indicating that there is a corresponding relationship between the resource and the cleanup function.

[0122] Since the second thread resource is a resource that can only be used by one thread, it needs to be stored in the TLS space.

[0123] If the resource is a third thread resource, the storage location is determined to be the heap space of the process. The storage method is determined as: storing the resource to the storage location; or combining the resource with the cleanup function to generate a second structure, storing the second structure to the TLS space, storing the resource to the storage location, and deleting the second structure.

[0124] Since the third thread resource is a resource that can be used by all threads in the process, and all threads in the process can access the heap space, it needs to be stored in the heap space.

[0125] It should be noted that, between storing the second structure in the TLS space and storing the resource in the storage location, a shield cancellation instruction and a release shield cancellation instruction may be added in sequence.

[0126] S204: Store the resource in a storage location according to the storage method.

[0127] In this step, after the thread determines the storage method and storage location, it stores the resource in the storage location according to the storage method.

[0128] It should be noted that after the thread stores the resource in the storage location according to the storage method, the thread can also assign a resource handle so that the thread or function can use the resource.

[0129] S205: Release the shielding cancellation instruction.

[0130] In this step, in order for the thread to execute the received cancel instruction, the thread needs to unblock the cancel instruction after storing the resource in the storage location according to the storage method.

[0131] It should be noted that, during the period of shielding the cancel instruction, if the thread receives a cancel instruction, the cancel instruction will be shelved until the shielding of the cancel instruction is released and then executed.

[0132] Exemplarily, the function used by the thread to implement the unblocking cancel instruction may be SIG_UNBLOCK signoSIGCANCEL.

[0133] For example, Figure 2bThe flowchart of thread management resources provided for this application is as follows: Figure 2b As shown, the thread first applies for resources, then uses the resources, and finally releases the resources.

[0134] The thread resource release method provided in this embodiment shields the cancel instruction before the thread applies for resources, and unshields the cancel instruction after storing the applied resources, so that the thread will not execute the cancel instruction during the resource application period, thereby avoiding resource leakage. In addition, by determining the storage location and storage method of the resource according to the type of resource, the thread resources can be released later, thereby avoiding memory leakage.

[0135] The following is an embodiment of the device of the present application, which can be used to execute the embodiment of the method of the present application. For details not disclosed in the embodiment of the device of the present application, please refer to the embodiment of the method of the present application.

[0136] Figure 3 A structural diagram of an embodiment of a thread resource release device provided in this application; Figure 3 As shown, the thread resource releasing device 30 includes:

[0137] The first resource releasing module 31 is used for:

[0138] When a thread exits, for each first thread resource in the thread local storage TLS space, the first thread resource is released according to a cleanup function corresponding to the first thread resource stored in the TLS space by the thread, where the first thread resource is a resource that can only be used by a thread function called by the thread;

[0139] When the thread exits, for each second thread resource in the TLS space, the second thread resource is released by the thread according to a cleanup function corresponding to the second thread resource stored in the TLS space, where the second thread resource is a resource that can only be used by the thread;

[0140] The second resource releasing module 32 is used to release the third thread resource in the heap space of the process through the operating system when the process to which the thread belongs exits, and the third thread resource is a resource that can be used by all threads in the process.

[0141] Further, the first thread resource and the cleanup function corresponding to the first thread resource are stored in a first structure in the TLS space;

[0142] The second thread resource and the cleanup function corresponding to the second thread resource are stored in a second structure in the TLS space.

[0143] Furthermore, the thread resource releasing device 30 further includes:

[0144] The third resource release module 33 is used to execute a release instruction through the thread when the thread function called by the thread ends execution, and the release instruction is used to release the first thread resources in each first structure in the function stack space of the thread function according to the cleanup function in the first structure, and delete the first structure from the TLS space.

[0145] Furthermore, the thread resource releasing device 30 further includes:

[0146] The compiling module 34 is used for inserting the release instruction after each thread function called by the thread when the compiler compiles the source code of the process when the function called by the thread ends execution and before the thread executes the release instruction.

[0147] Furthermore, the thread resource releasing device 30 further includes:

[0148] The resource application module 35 is used to:

[0149] Before applying for resources, the thread shields a cancel instruction, wherein the cancel instruction is used to exit the thread;

[0150] After applying for resources, the thread performs the following processing:

[0151] Determine the storage location and storage method of the resource according to the type of the resource;

[0152] Storing the resource in the storage location according to the storage method;

[0153] Unblock cancels the command.

[0154] Furthermore, the resource application module 35 is specifically used for:

[0155] If the resource is a first thread resource, determining the storage location to be the TLS space and a function stack space of a thread function using the resource, or determining the storage location to be the TLS space;

[0156] The storage method is determined as: combining the resource with a cleanup function to generate a first structure, and storing the first structure in a storage location.

[0157] Furthermore, the resource application module 35 is also specifically used for:

[0158] If the resource is a second thread resource, determining the storage location to be the TLS space;

[0159] The storage method is determined as: combining the resource with a cleanup function to generate a second structure, and storing the second structure in a storage location.

[0160] Furthermore, the resource application module 35 is also specifically used for:

[0161] If the resource is a third thread resource, determining the storage location to be a heap space of the process;

[0162] Determining the storage method is: storing the resource in the storage location; or,

[0163] The resource and the cleanup function are combined to generate a second structure, the second structure is stored in the TLS space, the resource is stored in the storage location, and the second structure is deleted.

[0164] The thread resource release device provided in this embodiment is used to execute the technical solution in any of the aforementioned method embodiments. Its implementation principle and technical effects are similar and will not be repeated here.

[0165] Figure 4 This is a schematic diagram of the structure of an electronic device provided in this application. Figure 4 As shown, the electronic device 40 includes:

[0166] Processor 41, memory 42, and communication interface 43;

[0167] The memory 42 is used to store executable instructions of the processor 41;

[0168] The processor 41 is configured to execute the technical solution in any of the aforementioned method embodiments by executing the executable instructions.

[0169] Optionally, the memory 42 may be independent or integrated with the processor 41 .

[0170] Optionally, when the memory 42 is a device independent of the processor 41, the electronic device 40 may further include:

[0171] The bus 44 , the memory 42 and the communication interface 43 are connected to the processor 41 via the bus 44 and communicate with each other. The communication interface 43 is used to communicate with other devices.

[0172] Optionally, the communication interface 43 may be implemented by a transceiver. The communication interface is used to implement communication between the database access device and other devices (such as a client, a read-write library, and a read-only library). The memory may include a random access memory (RAM) and may also include a non-volatile memory (non-volatile memory), such as at least one disk storage.

[0173] The bus 44 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.

[0174] The above-mentioned processor can be a general-purpose processor, including a central processing unit CPU, a network processor (NP), etc.; it can also be a digital signal processor DSP, an application-specific integrated circuit ASIC, a field programmable gate array FPGA or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

[0175] The electronic device is used to execute the technical solution in any of the aforementioned method embodiments, and its implementation principle and technical effect are similar and will not be repeated here.

[0176] An embodiment of the present application also provides a readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the technical solution provided by any of the aforementioned method embodiments is implemented.

[0177] An embodiment of the present application also provides a computer program product, including a computer program, which is used to implement the technical solution provided by any of the aforementioned method embodiments when executed by a processor.

[0178] Those skilled in the art can understand that all or part of the steps of implementing the above-mentioned method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk and other media that can store program codes.

[0179] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A thread resource release method, characterized in that: include: When a thread exits, the thread releases each first thread resource in the thread local storage TLS space according to a cleanup function corresponding to the first thread resource stored in the TLS space, where the first thread resource is a resource that can only be used by a thread function called by the thread; When the thread exits, the thread releases each second thread resource in the TLS space according to a cleanup function corresponding to the second thread resource stored in the TLS space, where the second thread resource is a resource that can only be used by the thread; When the process to which the thread belongs exits, the operating system releases the third thread resource in the heap space of the process, where the third thread resource is a resource that can be used by all threads in the process.

2. The method according to claim 1, characterized in that The first thread resource and the cleanup function corresponding to the first thread resource are stored in a first structure in the TLS space; The second thread resource and the cleanup function corresponding to the second thread resource are stored in a second structure in the TLS space.

3. The method according to claim 2, characterized in that The method further comprises: When the thread function called by the thread ends execution, the thread executes a release instruction, and the release instruction is used to release the first thread resources in each first structure in the function stack space of the thread function according to the cleanup function in the first structure, and delete the first structure from the TLS space.

4. The method according to claim 3, characterized in that When the function called by the thread ends execution and before the thread executes a release instruction, the method further includes: When compiling the source code of the process, the compiler inserts the release instruction after each thread function called by the thread.

5. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: Before applying for resources, the thread shields a cancel instruction, wherein the cancel instruction is used to exit the thread; After applying for resources, the thread performs the following processing: Determine the storage location and storage method of the resource according to the type of the resource; Storing the resource in the storage location according to the storage method; Unblock cancels the command.

6. The method according to claim 5, characterized in that Determining the storage location and storage method of the resource according to the type of the resource includes: If the resource is a first thread resource, determining the storage location to be the TLS space and a function stack space of a thread function using the resource, or determining the storage location to be the TLS space; The storage method is determined as: combining the resource with a cleanup function to generate a first structure, and storing the first structure in a storage location.

7. The method according to claim 6, characterized in that The method further comprises: If the resource is a second thread resource, determining the storage location to be the TLS space; The storage method is determined as: combining the resource with a cleanup function to generate a second structure, and storing the second structure in a storage location.

8. The method according to claim 7, characterized in that The method further comprises: If the resource is a third thread resource, determining the storage location to be a heap space of the process; Determining the storage method is: storing the resource in the storage location; or, The resource and the cleanup function are combined to generate a second structure, the second structure is stored in the TLS space, the resource is stored in the storage location, and the second structure is deleted.

9. A thread resource release device, characterized in that: include: The first resource release module is used to: When a thread exits, for each first thread resource in the thread local storage TLS space, the thread resource is released according to a cleanup function corresponding to the first thread resource stored in the TLS space, where the first thread resource is a resource that can only be used by a thread function called by the thread; When the thread exits, for each second thread resource in the TLS space, the second thread resource is released by the thread according to a cleanup function corresponding to the second thread resource stored in the TLS space, where the second thread resource is a resource that can only be used by the thread; The second resource release module is used to release the third thread resource in the heap space of the process through the operating system when the process to which the thread belongs exits, and the third thread resource is a resource that can be used by all threads in the process.

10. An electronic device, characterized in that: include: Processor, memory, communication interface; The memory is used to store executable instructions of the processor; The processor is configured to execute the thread resource releasing method according to any one of claims 1 to 8 by executing the executable instructions.

11. A readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the thread resource releasing method according to any one of claims 1 to 8 is implemented.

12. A computer program product, characterized in that It comprises a computer program, which is used to implement the thread resource releasing method according to any one of claims 1 to 8 when executed by a processor.

Citation Information

Patent Citations

  • Resource calling method based on thrift framework threadpool mode

    CN112433865A

  • Memory leak processing method and device, electronic equipment and computer storage medium

    CN113835992A

  • Embedded real-time operating system simulation method and device and storage medium

    CN116795481A

  • Video memory monitoring method and device

    CN116954742A

  • Task scheduling method and device, computing equipment and storage medium

    CN119376867A