Thread resource release method, device, equipment, medium and program product
By automatically releasing thread resources by using the cleaning function in the TLS space when a thread exits, and the operating system releases heap space resources when a process exits, the problem of resource leakage in the existing technology is solved and system stability and development efficiency is improved.
Patent Information
- Application Number
- CN202510286633.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-12
- Publication Date
- 2025-08-22
- Estimated Expiration
- 2045-03-12
AI Technical Summary
In the prior art, thread resource release methods require staff to manually write code instructions or functions, which are prone to writing errors or forgetting to write, resulting in resource leakage.
When a thread exits, the thread resources are automatically released through the thread's local storage cleaning function in the TLS space; when the process exits, the operating system releases the process's heap space resources, and uses the corresponding relationship between the structure storage resources and the cleaning function to achieve automatic resource release.
Avoid resource leakage, improve system stability, performance and security, reduce developer workload, and improve code maintainability and resource utilization.
Smart Images

Figure CN119781995B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a thread resource release method, apparatus, device, medium, and program product. Background Art
[0002] In a computer operating system, each program runs as a process, and within each process, multiple threads execute. Before and during execution, threads can request resources to implement their functionality. Resources can include address space, file descriptors, semaphores, and more. To prevent resource leaks, thread resources must be released when a thread exits.
[0003] In the prior art, thread exits are categorized as normal exits and exits due to a cancel instruction. In a normal exit, a thread executes code instructions written by the staff to release resources, such as free and close. In the case of an exit instruction, a thread executes functions written by the staff to release resources, such as pthread_cleanup_push and pthread_cleanup_pop.
[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, the first thread resource and the 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 one possible implementation, the method further includes:
[0013] When the thread function called by the thread ends, 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 one 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] Determining 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, 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 the function stack space of the 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 and the cleanup function to generate a first structure, and storing the first structure to a storage location.
[0025] In one possible implementation, the method further includes:
[0026] If the resource is a second thread resource, determining that the storage location is the TLS space;
[0027] The storage method is determined as: combining the resource and the cleanup function to generate a second structure, and storing the second structure in a storage location.
[0028] In one possible implementation, the method further includes:
[0029] If the resource is a third thread resource, determining that the storage location is 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, comprising:
[0033] The first resource release module is configured to:
[0034] When a thread exits, for each first thread resource in the thread-local storage TLS space, the thread releases the first thread resource 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;
[0035] When the thread exits, for each second thread resource in the TLS space, the thread releases the second thread resource 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 releasing 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. 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 according to 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, apparatus, device, medium, and program product provided by embodiments of the present application utilize a method, apparatus, device, and program product. Upon thread exit, the thread releases each resource in the thread-local storage space that is only usable by a thread function, according to the cleanup function corresponding to that resource in the thread-local storage space. Furthermore, each resource in the thread-local storage space that is only usable by a thread is released according to the cleanup function corresponding to that resource in the local storage space. Upon process exit, the operating system releases resources in the heap space that are usable by all threads in the process. This solution eliminates resource leaks by releasing the three thread resources upon thread exit and during exit, 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 the first embodiment of the thread resource release method provided by this application;
[0046] Figure 1b A schematic diagram of the use of the third thread resource provided by 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 the third embodiment of the thread resource release method provided in this application;
[0050] Figure 2b A flowchart of the thread management resources provided for this application;
[0051] Figure 3 A structural diagram of an embodiment of a thread resource release device provided by this application;
[0052] Figure 4 This is a schematic diagram of the structure of an electronic device provided in this application.
[0053] The above drawings illustrate specific embodiments of the present application, which will be described in more detail below. These drawings and the textual description are not intended to limit the scope of the present application in any way, but rather to illustrate the concepts of the present application to those skilled in the art by reference to specific embodiments. DETAILED DESCRIPTION
[0054] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all embodiments consistent with the present application. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present application, as detailed in the appended claims.
[0055] The terms "first," "second," "third," "fourth," and so forth (if any) in the specification and claims of this application and in the accompanying drawings are used to distinguish similar objects and are not necessarily used to describe a particular order or sequential sequence. It should be understood that the terms used in this manner are interchangeable where appropriate, so that the embodiments of the application described herein can be implemented in orders 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 apparatus comprising a series of steps or elements is not necessarily limited to those steps or elements explicitly listed, but may include other steps or elements not explicitly listed or inherent to such process, method, product, or apparatus.
[0056] In a computer operating system, each program runs as a process, and within each process, multiple threads execute. Before and during execution, threads can request resources to implement their functionality. These resources can include address space, file descriptors, semaphores, thread locks, and more. To prevent resource leaks, thread resources must be released when a thread exits.
[0057] Since the same cleanup program 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] Thread exits can be classified into normal exits and exits due to a cancel instruction. When a thread exits normally, it executes the code instructions written by the staff to release resources, such as free and close. When a thread receives an exit instruction, the cancel instruction can be pthread_cancel, and it executes the functions written by the staff to release 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, thereby causing resource leakage.
[0060] In response to the problems existing in the prior art, the inventors discovered during their research on thread resource release methods that, in order to reduce the occurrence of resource leakage, it is possible to automatically release thread resources when the thread exits and the process exits, rather than requiring staff to write corresponding code instructions or functions for each thread. Thread resources are divided into three types. The first type is resources that can only be used by thread functions called by a thread. These can be combined with a cleanup function to form a first structure stored in the thread's thread local storage (TLS) space. The second type is resources that can only be used by a thread. These can be combined with a cleanup function to form a second structure stored in the TLS space. The third type is resources that can be used by all threads in a process and can be stored in the process's heap space. A thread's TLS space can only be accessed by that thread, and each thread of a process can access the process's heap space.
[0061] The staff can insert a release routine into the cleanup routine so that the release routine is 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. 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. The second thread resource is a resource that can only be used by the thread. The heap space of the process to which the thread belongs stores a third thread resource. The third thread resource is a resource that can be used by all threads in the process. There is a correspondence between the first thread resource and the cleanup function in the first structure, and there is a correspondence between the second thread resource and the cleanup function in the second structure.
[0064] Because the staff inserted 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 this scenario. It can be a computer, server, terminal device, 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 flow chart of the first embodiment of the thread resource release method provided by this application. This embodiment of the application describes how a thread releases resources of thread functions that can only be called by it, as well as resources that can only be used by itself, through a cleanup function when it exits; and how the operating system releases resources that can be used by all threads in the process when the process exits. 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 thread functions 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. This is called a 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. It 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 of resource is available to all threads in a process and can be stored in the process's heap space. This is called a third thread resource. A thread's TLS space is accessible only to that thread, while every thread in a 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 can be cleanup, close, free, etc. The embodiment of this application does not limit the cleanup function and can be determined according to actual conditions.
[0077] For example, the format of the first structure can 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, the release program will be executed during the execution of the cleanup program when the thread exits, that is, the thread releases each second thread resource in the TLS space according to the 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 can be: step S101 first, then step S102; step S102 first, then step S101; or step S101 and step S102 are executed simultaneously. This application does not limit the execution order of step S101 and step S102, and it can be determined according to actual circumstances.
[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 involves, when a thread exits, releasing resources in a first structure that can only be used by thread functions called by the thread according to a cleanup function in a first structure in the thread's local storage space; and releasing resources in a second structure that can only be used by the thread according to a cleanup function in a second structure in the local storage space; and when a process exits, the operating system releases resources in the heap space that are available to all threads in the process. Compared to the prior art method that requires staff to write code instructions or functions for each thread, this solution releases the three thread resources when the thread exits and exits, eliminating resource occupancy and resource leaks, thereby improving system stability, performance, and 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 written the thread function code, they need to write the code instructions to release the resources. For example, Figure 1d The flowchart of writing thread code provided by 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 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.
[0100] Therefore, when the thread function called by the thread ends, 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 to exit, since the thread function has not been executed yet, the first thread resources will not be released when the thread function execution 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] For example, 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 at the end of the thread function, thereby making the resource release more timely, reducing the resource occupation time, and improving resource utilization.
[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 it receives 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 a thread to implement the block 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 when the cancel instruction is blocked, so resource application during this period will not cause resource leakage. After the thread blocks 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 this 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 requests a resource, it can determine the resource's usage permissions and, therefore, the resource's type. When the resource's usage permissions are for thread function use, the resource's type is a first-thread resource; when the resource's usage permissions are for individual thread use, the resource's type is a second-thread resource; and when the resource's usage permissions are for multi-thread use, the resource's type is a 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 as: combining the resource and 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 a 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 the thread function called by the 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 in 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 process's heap space. The storage method is determined to be: storing the resource in the storage location; or combining the resource with a cleanup function to generate a second structure, storing the second structure in the TLS space, storing the resource in 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, it 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 cancellation instruction, the thread needs to unblock the cancellation instruction after storing the resource to the storage location according to the storage method.
[0131] It should be noted that, during the period when the cancel instruction is blocked, if a thread receives a cancel instruction, the cancel instruction will be put on hold until the cancel instruction is unblocked and executed.
[0132] Exemplarily, the function used by the thread to implement the unblock 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 prevents resource leaks by blocking cancel instructions before a thread applies for a resource and unblocking the cancel instructions after the applied resource is stored. This prevents the thread from executing cancel instructions during the resource application process. Furthermore, by determining the storage location and storage method based on the resource type, thread resources can be subsequently released, preventing memory leaks.
[0135] The following are device embodiments of the present application, which can be used to implement the method embodiments of the present application. For details not disclosed in the device embodiments of the present application, please refer to the method embodiments of the present application.
[0136] Figure 3 This is a structural diagram of an embodiment of a thread resource release device provided by this application; Figure 3 As shown, the thread resource releasing device 30 includes:
[0137] The first resource releasing module 31 is configured to:
[0138] When a thread exits, for each first thread resource in the thread-local storage TLS space, the thread releases the first thread resource 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;
[0139] When the thread exits, for each second thread resource in the TLS space, the thread releases the second thread resource 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 configured 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. The third thread resource is a resource that can be used by all threads in the process.
[0141] Furthermore, 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 execution of the thread function called by the thread ends. 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 configured to insert the release instruction after each thread function called by the thread when compiling the source code of the process through the compiler 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 through the thread, the following processing is performed:
[0151] Determining 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 configured to:
[0155] If the resource is a first thread resource, determining the storage location to be the TLS space and the function stack space of the 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 and the cleanup function to generate a first structure, and storing the first structure to a storage location.
[0157] Furthermore, the resource application module 35 is further configured to:
[0158] If the resource is a second thread resource, determining that the storage location is the TLS space;
[0159] The storage method is determined as: combining the resource and the cleanup function to generate a second structure, and storing the second structure in a storage location.
[0160] Furthermore, the resource application module 35 is further configured to:
[0161] If the resource is a third thread resource, determining that the storage location is 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 described in detail here.
[0165] Figure 4 This is a schematic diagram of the structure of an electronic device provided by 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 can 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, communication interface 43 may be implemented using a transceiver. The communication interface is used to enable communication between the database access device and other devices (e.g., clients, read-write libraries, and read-only libraries). The memory may include random access memory (RAM) and may also include non-volatile memory, such as at least one disk storage device.
[0173] Bus 44 may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. Buses can be categorized as address buses, data buses, and control buses. For ease of illustration, the figure uses only one thick line, but this does not imply that there is only one bus or only 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, and discrete hardware components.
[0175] The electronic device is used to execute the technical solution in any of the aforementioned method embodiments, and its implementation principles and technical effects are similar and will not be repeated here.
[0176] An embodiment of the present application further provides a readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the technical solution provided by any of the aforementioned method embodiments.
[0177] An embodiment of the present application also provides a computer program product, including a computer program, which, when executed by a processor, is used to implement the technical solution provided by any of the aforementioned method embodiments.
[0178] Those skilled in the art will appreciate that all or part of the steps in the above-described method embodiments can be implemented using hardware associated with program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments. The aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[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 them. 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; Different thread resource types correspond to different storage locations and storage methods. The resource type is determined based on the resource usage permissions. 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; The method further comprises: When compiling the source code of the process, the compiler inserts a release instruction after each thread function called by the thread; When the thread function called by the thread ends, 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.
2. The method according to claim 1, 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: Determining 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.
3. The method according to claim 2, 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 the function stack space of the thread function using the resource, or determining the storage location to be the TLS space; The storage method is determined as: combining the resource and the cleanup function to generate a first structure, and storing the first structure to a storage location.
4. The method according to claim 3, characterized in that The method further comprises: If the resource is a second thread resource, determining that the storage location is the TLS space; The storage method is determined as: combining the resource and the cleanup function to generate a second structure, and storing the second structure in a storage location.
5. The method according to claim 4, characterized in that The method further comprises: If the resource is a third thread resource, determining that the storage location is 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.
6. A thread resource release device, characterized in that: include: The first resource release module is configured 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 thread releases the second thread resource 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; A second resource release module is configured to release, through the operating system, a third thread resource in the heap space of the process when the process to which the thread belongs exits, the third thread resource being a resource that can be used by all threads in the process; Different thread resource types correspond to different storage locations and storage methods. The resource type is determined based on the resource usage permissions. 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; The first resource release module is further configured to: When compiling the source code of the process, the compiler inserts a release instruction after each thread function called by the thread; When the thread function called by the thread ends, 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.
7. 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 5 by executing the executable instructions.
8. 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 5 is implemented.
9. A computer program product, characterized in that The invention comprises a computer program, which is used to implement the thread resource release method according to any one of claims 1 to 5 when executed by a processor.
Citation Information
Patent Citations
Embedded real-time operating system simulation method and device and storage medium
CN116795481A