Multi-thread service exiting method and device, storage medium and computer device
By using a combination of the status bit and count bit of a single record atomic variable in a multi-threaded service, the logical conflict when the interface service stops is resolved, achieving efficient and stable service exit, avoiding livelock, and improving operating efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- GUANGZHOU WERIDE TECH LTD CO
- Filing Date
- 2023-02-13
- Publication Date
- 2026-05-08
AI Technical Summary
In a multi-threaded environment, when the interface service stops, it overlaps with the business logic, causing the program to crash. Traditional locking solutions affect the running efficiency.
A single record atomic variable is used to initialize and update the atomic variable through a combination of status bits and counter bits to resolve logical conflicts when a multi-threaded service exits, avoid livelock problems, and ensure the atomicity of operations by using atomic operations.
It improves the stability and efficiency of multi-threaded service exit, avoids livelock issues, and ensures the robustness and accuracy of the service.
Smart Images

Figure CN116028189B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, storage medium, and computer device for exiting a multi-threaded service. Background Technology
[0002] With the development of computer technology, many software programs provide services by exposing interfaces. However, many software programs run in a multi-threaded environment. When an interface service stops working, other threads may continue to call that service. The logic for stopping and exiting the interface service and the business logic of the interface itself can overlap, causing program crashes. The traditional solution is to use locking, but for frequently called interfaces, frequent locking and unlocking can negatively impact performance. Summary of the Invention
[0003] The purpose of this application is to address at least one of the aforementioned technical deficiencies, particularly the technical deficiency that the robustness test results in the prior art are not accurate enough.
[0004] In a first aspect, embodiments of this application provide a method for exiting a multi-threaded service, including:
[0005] Initialize the record atomic variables to clear the counter bits and status bits to indicate that the target service is in a running state;
[0006] In response to any thread's request to call the target service, retrieve the status bit;
[0007] Determine if the status bit indicates that the target service is in a stopped state. If yes, return an error call message. If no, increment the counter by the first value and execute the business logic corresponding to the target service. After the business logic is executed, decrement the counter by the first value.
[0008] In response to a service stop request for the target service, the status bit is updated to indicate that the target service is in a stopped state, and the exit logic corresponding to the target service is executed when the counter bit is cleared to zero.
[0009] In one embodiment, the recorded atomic variable is a multi-digit integer atomic variable, the least significant bit of the recorded atomic variable is the status bit, and the remaining bits of the recorded atomic variable are count bits.
[0010] In one embodiment, obtaining the status bit includes:
[0011] Read and record atomic variables;
[0012] Perform a bitwise AND operation between the read result and 1 to obtain the status bit.
[0013] In one embodiment, the first value is 2.
[0014] In one embodiment, when the status bit is equal to 1, the status bit indicates that the target service is in a stopped state, and when the status bit is equal to 0, the status bit indicates that the target service is in a running state.
[0015] In one embodiment, incrementing the counter by a first value includes:
[0016] Use the atomic increment operation to increment the counter by the first value.
[0017] In one embodiment, decreasing the counter by a first value includes:
[0018] Use atomic subtraction operations to decrease the counter by the first value.
[0019] Secondly, embodiments of this application provide a multi-threaded service exit device, comprising:
[0020] The initialization module is used to initialize the record atomic variables so that the count bits of the record atomic variables are cleared and the status bits indicate that the target service is in a running state;
[0021] The first processing module is used to respond to any thread's call request to the target service and obtain the status bit;
[0022] The second processing module is used to determine whether the status bit indicates that the target service is in a stopped state. If yes, it will return an error call information. If no, it will increment the counter by a first value and execute the business logic corresponding to the target service. After the business logic is executed, it will decrement the counter by a first value.
[0023] The third processing module is used to update the status bit in response to a stop service request for the target service, so that the status bit indicates that the target service is in a stopped state, and to execute the exit logic corresponding to the target service when the counter bit is cleared to zero.
[0024] Thirdly, embodiments of this application provide a computer device, including one or more processors and a memory, in which computer-readable instructions are stored. When the computer-readable instructions are executed by one or more processors, the steps of the multi-threaded service exit method in any of the above embodiments are performed.
[0025] Fourthly, embodiments of this application provide a storage medium storing computer-readable instructions. When executed by one or more processors, the computer-readable instructions cause the one or more processors to perform the steps of the multi-threaded service exit method in any of the above embodiments.
[0026] As can be seen from the above technical solutions, the embodiments of this application have the following advantages:
[0027] Based on any of the above embodiments, a record atomic variable containing a status bit and a count bit is first set. This record atomic variable is initialized before the target service starts providing services externally, indicating that the target service currently has zero service objects and is in a running state. When processing any thread's call request to the target service, if the status bit reflects that the target service is in a stopped state, an error call information is directly fed back to the requester, and the call request cannot be processed. If the status bit reflects that the target service is in a running state, the number of objects currently being served by the target service is counted by modifying the count bit, and the requester's call request is processed according to the original business logic of the target service. When the target service receives a stop service request, the status bit is adjusted to indicate that the target service is in a stopped state. The target service will not provide services to new call requests, and the exit logic of the target service is executed after all current call processing is completed based on the count bit statistics. The method in this embodiment solves the logical conflict problem when a multi-threaded service exits using a lock-free approach. Using a single record atomic variable instead of two independent atomic variables avoids the livelock problem that may occur during multi-threaded variable operations, greatly improving the stability and operational efficiency of multi-threaded service exit. Attached Figure Description
[0028] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0029] Figure 1 A flowchart illustrating a server-side testing method provided in one embodiment of this application;
[0030] Figure 2 A flowchart illustrating a server-side testing method provided in another embodiment of this application;
[0031] Figure 3 This is an internal structural diagram of a computer device provided in an embodiment of this application. Detailed Implementation
[0032] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0033] Firstly, this application provides a method for exiting a multi-threaded service; please refer to [link to relevant documentation]. Figure 1 This includes steps S102 to S108.
[0034] S102, initialize the record atomic variables so that the count bit of the record atomic variables is cleared and the status bit indicates that the target service is in running state.
[0035] It's understandable that in a multi-threaded environment, each thread might need to modify the value of a variable. Modifying a variable requires three steps: reading, modifying, and writing back. While one thread is executing one of these steps, the variable might also be being processed by other threads, ultimately leading to unexpected changes. Using atomic variables in a program ensures that any thread operating on an atomic variable is not interfered with by other threads. The atomic variable used in this embodiment is the one used to record the status of the interface service. The atomic variable is a multi-bit binary variable, with each bit including a counter bit and a status bit. The status bit indicates whether the target service is currently running or stopped. A running target service can provide services to the outside world through the interface, while a stopped target service cannot. The counter bit represents the number of objects currently being served when the target service is running. During the initialization phase of the target service, the atomic variable is initialized first. In the initialized state, the target service has zero service objects; therefore, the counter bit of the atomic variable is cleared. To make the target service callable, initialization must ensure that the status bit indicates the target service is running. Combining the status bit and the count bit into a single atomic variable, rather than using two separate atomic variables to represent the target service's status and count, is because these two atomic variables need to be processed multiple times in subsequent counting and exit logic. This process can easily lead to livelock, making it impossible to achieve the goal of safely exiting the service. Using a single record atomic variable with different bits representing the status and count solves this problem.
[0036] S104: In response to any thread's call request to the target service, obtain the status bit.
[0037] It is understandable that if a target service needs to be called, it is necessary to determine that the target service is currently running. Therefore, relevant code for obtaining the status bit can be added to the target service's program. When the target service receives a call request, it first obtains the status bit.
[0038] S106, determine whether the status bit indicates that the target service is in a stopped state. If yes, then report an error call information. If no, then increment the counter by the first value and execute the business logic corresponding to the target service. After the business logic is executed, decrement the counter by the first value.
[0039] It's understandable that after obtaining the status bit, if the status bit indicates that the target service is in a stopped state, an error call information should be directly sent to the requester, informing them that the target service is currently unavailable. In this case, the target service can eventually send an error call information to the requester via a return instruction, and the entire program execution of the target service ends, providing no service to the requester. If the status bit indicates that the target service is in a running state, the counter bit of the atomic variable should be updated. Each time a service object is served, the counter bit increases by a first value. The number of objects currently being served by the target service can be determined based on the multiple relationship between the counter bit and the first value. Since the status bit indicates that the target service is in a running state and should provide corresponding services to the requester, the corresponding business logic code of the target service also needs to be executed to handle the requester's call request. After the business logic code is executed, it means that the requester's request has been completed, and the previously increased first value should be subtracted from the counter bit, indicating that the requester is no longer an object being served by the target service.
[0040] S108, in response to the service stop request for the target service, update the status bit so that the status bit indicates that the target service is in a stopped state, and execute the exit logic corresponding to the target service when the counter bit is cleared to zero.
[0041] When the interface corresponding to the target service receives a stop service request, it sets the status bit to indicate that the target service is in a stopped state. After this, as shown in step S106, any subsequent call request from any requester will receive an error call message because the target service is in a stopped state, and no new business operation will occur. At this point, if the counter bit is not yet cleared, it means that some call requests received before the stop service request have not yet been processed. If the exit logic code of the target service is executed directly, a conflict between the business operation logic and the business exit logic will occur, leading to program crashes. Therefore, it is necessary to wait for the counter bit to clear, that is, for all current call requests to be processed, before executing the exit logic corresponding to the target service.
[0042] Based on the multi-threaded service exit method in this embodiment, a record atomic variable containing a status bit and a count bit is first set. This record atomic variable is initialized before the target service starts providing services externally, indicating that the target service currently has zero service objects and is in a running state. When processing any thread's call request to the target service, if the status bit reflects that the target service is in a stopped state, an error call information is directly fed back to the requester, and the call request cannot be processed. If the status bit reflects that the target service is in a running state, the number of objects currently being served by the target service is counted by modifying the count bit, and the requester's call request is processed according to the original business logic of the target service. When the target service receives a stop service request, the status bit is adjusted to indicate that the target service is in a stopped state. The target service will not provide services to new call requests, and the exit logic of the target service is executed after all current call processing is completed based on the count bit statistics. The method in this embodiment solves the logical conflict problem during multi-threaded service exit through a lock-free approach. Using a single record atomic variable instead of two independent atomic variables avoids the livelock problem that may occur during multi-threaded variable operations, greatly improving the stability and operational efficiency of multi-threaded service exit.
[0043] In one embodiment, the recorded atomic variable is a multi-digit integer atomic variable, the least significant bit of the recorded atomic variable is the status bit, and the remaining bits of the recorded atomic variable are count bits.
[0044] It's understandable that the target service has at least two states: running and stopped, which can be represented by a single binary digit. The simplest approach is to select the lowest bit in the atomic variable as the status bit, and the others as counter bits. This way, when operating on the atomic variable, alternating increment and decrement operations modify only the status bit without affecting the counter bits. Similarly, incrementing and decrementing the counter bits by two can modify only the counter bits without affecting the status bit; for example, the first value can be 2. As an extended approach, the highest bit can be used as the status bit, and the others as counter bits. Adding or subtracting a preset value allows modification of only the status bit without affecting the counter bits. Let n represent the number of bits in the atomic variable, and the preset value be a binary number where the nth bit is 1 and the rest are 0. For the counter bits, before all bits are 1, incrementing and decrementing operations can modify only the counter bits without affecting the status bit. There are many similar extension schemes. For example, the lowest or highest two bits can be selected as status bits to add a "completed stop" state to the target service. After the exit logic of the target service is completed, the status bits will indicate that the target service is in the "completed stop" state. In step S106, it should be determined whether the status bits indicate that the target service is in a stopped state or a "completed stop" state. Alternatively, the middle bit of the atomic variable could be used to select the status bits, but this would make the logic for updating the counter and status bits very complex.
[0045] In one embodiment, obtaining the status bit includes:
[0046] (1) Read the record atomic variables.
[0047] (2) Perform a bitwise AND operation between the read result and 1 to obtain the status bit.
[0048] It is understandable that, since the status bit is part of the recorded atomic variable, to obtain the status bit, the recorded atomic variable must first be read from the storage space. When any binary number is ANDed with 1, its least significant bit will be retained. Therefore, when selecting the least significant bit of the recorded atomic variable as the status bit, performing a AND operation with 1 will yield the current status bit. After obtaining the current status bit, it may be equal to 0 or 1. These two values correspond to a state of the target service. In one embodiment, when the status bit is equal to 1, it indicates that the target service is in a stopped state; when the status bit is equal to 0, it indicates that the target service is in a running state. Alternatively, the status bit can be equal to 0, indicating that the target service is in a stopped state, and equal to 1, indicating that the target service is in a running state.
[0049] In one embodiment, incrementing the counter bit by a first value includes: incrementing the counter bit by a first value using an atomic increment operation.
[0050] In one embodiment, reducing the count bit by a first value includes: reducing the count bit by a first value using an atomic subtraction operation.
[0051] It's understandable that atomic operations should be used when modifying atomic variables, as these operations are not interrupted by thread scheduling mechanisms. Different programming languages have different implementations of atomic operations. For example, Java has many built-in atomic operation classes based on the CAS (Compare and Swap) mechanism, which can be used directly when writing relevant code. Therefore, you can choose the appropriate code to implement atomic increment and decrement operations based on the programming language used in your specific business logic.
[0052] This application provides a multi-threaded service exit device. Please refer to [link / reference]. Figure 2 It includes an initialization module 210, a first processing module 220, a second processing module 230, and a third processing module 240.
[0053] The initialization module 210 is used to initialize the record atomic variables so that the count bits of the record atomic variables are cleared and the status bits indicate that the target service is in a running state.
[0054] The first processing module 220 is used to respond to any thread's call request to the target service and obtain the status bit.
[0055] The second processing module 230 is used to determine whether the status bit indicates that the target service is in a stopped state. If so, it will return an error call information. If not, it will increment the counter by a first value and execute the business logic corresponding to the target service. After the business logic is executed, it will decrement the counter by a first value.
[0056] The third processing module 240 is used to update the status bit in response to a stop service request for the target service, so that the status bit indicates that the target service is in a stopped state, and execute the exit logic corresponding to the target service when the counter bit is cleared to zero.
[0057] In one embodiment, the recorded atomic variable is a multi-digit integer atomic variable, with the least significant bit being the status bit and the remaining bits being count bits. The first processing module 220 is used to read the recorded atomic variable; and performs a bitwise AND operation between the read result and 1 to obtain the status bit.
[0058] In one embodiment, the second processing module 230 is used to increment the counter bit by a first value using an atomic increment operation.
[0059] In one embodiment, the second processing module 230 is used to reduce the count bit by a first value using an atomic subtraction operation.
[0060] Specific limitations regarding the multi-threaded service exit device can be found in the limitations of the multi-threaded service exit method described above, and will not be repeated here. Each module in the aforementioned multi-threaded service exit device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in the computer device in hardware form, or stored in the memory of the computer device in software form, so that the processor can call and execute the operations corresponding to each module. It should be noted that the module division in this embodiment is illustrative and only represents a logical functional division; other division methods may be used in actual implementation.
[0061] This application provides a computer device including one or more processors and a memory. The memory stores computer-readable instructions. When the computer-readable instructions are executed by one or more processors, they perform the following: initializing a record atomic variable to clear its counter bit and a status bit indicating that the target service is in a running state; in response to a call request from any thread to the target service, obtaining the status bit; determining whether the status bit indicates that the target service is in a stopped state, and if so, feeding back an error call message; if not, incrementing the counter bit by a first value and executing the business logic corresponding to the target service, and decrementing the counter bit by a first value after the business logic is executed; and in response to a service stop request for the target service, updating the status bit to indicate that the target service is in a stopped state, and executing the exit logic corresponding to the target service when the counter bit is cleared.
[0062] In one embodiment, when the computer-readable instructions are executed by one or more processors, the following are performed: read the record atomic variable; and perform a bitwise AND operation between the read result and 1 to obtain a status bit.
[0063] In one embodiment, when the computer-readable instructions are executed by one or more processors, the following is performed: incrementing the counter bit by a first value using an atomic increment operation.
[0064] In one embodiment, when the computer-readable instructions are executed by one or more processors, the following is performed: the counter bits are reduced by a first value using an atomic subtraction operation.
[0065] Indicatively, such as Figure 3 As shown, Figure 3 This is a schematic diagram of the internal structure of a computer device 300 provided in an embodiment of this application. The computer device 300 can be provided as a server. (Refer to...) Figure 3The computer device 300 includes a processing component 302, which further includes one or more processors, and memory resources represented by memory 301 for storing instructions, such as application programs, that can be executed by the processing component 302. The application programs stored in memory 301 may include one or more modules, each corresponding to a set of instructions. Furthermore, the processing component 302 is configured to execute instructions to perform the steps of the multi-threaded service exit method of any of the above embodiments.
[0066] The computer device 300 may also include a power supply component 303 configured to perform power management of the computer device 300, a wired or wireless network interface 304 configured to connect the computer device 300 to a network, and an input / output (I / O) interface 305. The computer device 300 may operate on an operating system stored in memory 301, such as Windows Server™, Mac OS X™, Unix™, Linux™, Free BSD™, or similar.
[0067] Those skilled in the art will understand that Figure 3 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0068] This application provides a storage medium storing computer-readable instructions. When executed by one or more processors, the computer-readable instructions cause the one or more processors to perform the following: initialize a record atomic variable to clear its counter bit and a status bit indicating that the target service is in a running state; in response to a call request from any thread to the target service, obtain the status bit; determine whether the status bit indicates that the target service is in a stopped state; if so, return an error call message; if not, increment the counter bit by a first value and execute the business logic corresponding to the target service; and after the business logic is executed, decrement the counter bit by a first value; in response to a service stop request for the target service, update the status bit to indicate that the target service is in a stopped state, and execute the exit logic corresponding to the target service when the counter bit is cleared.
[0069] In one embodiment, when the computer-readable instructions are executed by one or more processors, the one or more processors perform the following: read the record atomic variable; and perform a bitwise AND operation between the read result and 1 to obtain a status bit.
[0070] In one embodiment, when the computer-readable instructions are executed by one or more processors, the one or more processors perform the following: incrementing the counter bit by a first value using an atomic increment operation.
[0071] In one embodiment, when the computer-readable instructions are executed by one or more processors, the one or more processors perform the following: using an atomic subtraction operation to reduce the count bit by a first value.
[0072] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0073] The various embodiments in this specification are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The various embodiments can be combined as needed, and the same or similar parts can be referred to each other.
[0074] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for exiting a multi-threaded service, characterized in that, include: Initialize the record atomic variables so that the count bit of the record atomic variables is cleared and the status bit indicates that the target service is in a running state; In response to any thread's call request to the target service, the status bit is obtained; Determine whether the status bit indicates that the target service is in a stopped state. If yes, then return an error call information. If no, then increment the counter bit by a first value and execute the business logic corresponding to the target service. After the business logic is executed, decrement the counter bit by the first value. In response to a service stop request for the target service, the status bit is updated so that the status bit indicates that the target service is in a stopped state, and the exit logic corresponding to the target service is executed when the counter bit is cleared to zero. The target service is used to provide services to the outside world through an interface; the record atomic variable is a multi-bit binary variable, and each bit of the record atomic variable includes the count bit and the status bit; the status bit is used to indicate whether the target service is currently in a running state or a stopped state; the count bit represents the number of objects currently being served by the target service when it is in a running state; The atomic variable is used to ensure that when any thread operates on the atomic variable, it will not be interfered with by other threads.
2. The multi-threaded service exit method according to claim 1, characterized in that, The recorded atomic variable is a multi-digit integer atomic variable, the least significant bit of the recorded atomic variable is the status bit, and the remaining bits of the recorded atomic variable are the count bits.
3. The multi-threaded service exit method according to claim 2, characterized in that, The process of obtaining the status bit includes: Read the recorded atomic variables; Perform a bitwise AND operation between the read result and 1 to obtain the status bit.
4. The multi-threaded service exit method according to claim 2, characterized in that, The first value is 2.
5. The multi-threaded service exit method according to claim 4, characterized in that, When the status bit is equal to 1, the status bit indicates that the target service is in the stopped state; when the status bit is equal to 0, the status bit indicates that the target service is in the running state.
6. The multi-threaded service exit method according to claim 1, characterized in that, Increasing the counter by a first value includes: The first value is increased by using an atomic increment operation.
7. The multi-threaded service exit method according to claim 1, characterized in that, Decrease the counter by the first value, including: The first value is reduced by using an atomic subtraction operation.
8. A multi-threaded service exit device, characterized in that, include: An initialization module is used to initialize the record atomic variables so that the count bits of the record atomic variables are cleared and the status bits indicate that the target service is in a running state; The first processing module is used to obtain the status bit in response to a call request from any thread to the target service; The second processing module is used to determine whether the status bit indicates that the target service is in a stopped state. If yes, it will return an error call information. If no, it will increment the counter bit by a first value and execute the business logic corresponding to the target service. After the business logic is executed, it will decrement the counter bit by the first value. The third processing module is used to update the status bit in response to a service stop request for the target service, so that the status bit indicates that the target service is in a stopped state, and to execute the exit logic corresponding to the target service when the counter bit is cleared to zero. The target service is used to provide services to the outside world through an interface; the record atomic variable is a multi-bit binary variable, and each bit of the record atomic variable includes the count bit and the status bit; the status bit is used to indicate whether the target service is currently in a running state or a stopped state; the count bit represents the number of objects currently being served by the target service when it is in a running state; The atomic variable is used to ensure that when any thread operates on the atomic variable, it will not be interfered with by other threads.
9. A computer device, characterized in that, It includes one or more processors and a memory storing computer-readable instructions that, when executed by the one or more processors, perform the steps of the multi-threaded service exit method as described in any one of claims 1-7.
10. A storage medium, characterized in that, The storage medium stores computer-readable instructions that, when executed by one or more processors, cause the one or more processors to perform the steps of the multi-threaded service exit method as described in any one of claims 1-7.
Citation Information
Patent Citations
Methods and apparatus for managing deactivation and shutdown of a server
US5907675A