CN-116028189-B - Multithreading service exit method, device, storage medium and computer equipment
Abstract
The application provides a multithreading service exit method, a multithreading service exit device, a storage medium and computer equipment. The method comprises the steps of initializing a record atom variable to enable a counting bit of the record atom variable to be cleared and a state bit to represent that a target service is in an operation state, responding to a call request of any thread to the target service to obtain the state bit, judging whether the state bit represents that the target service is in a stop state, feeding back error call information if yes, enabling the counting bit to be increased by a first numerical value, executing business logic corresponding to the target service, enabling the counting bit to be decreased by the first numerical value after the business logic is executed, and updating the state bit to enable the state bit to represent that the target service is in the stop state in response to a stop service request aiming at the target service, and executing exit logic corresponding to the target service under the condition that the counting bit is cleared. The method solves the problem of service exit logic conflict through a lock-free scheme, and improves the operation efficiency.
Inventors
- LIU YUAN
Assignees
- 广州文远知行科技有限公司
Dates
- Publication Date
- 20260508
- Application Date
- 20230213
Claims (10)
- 1. A method of multi-threaded service exit, comprising: Initializing a record atom variable, so that a count bit of the record atom variable is cleared and a state bit indicates that a target service is in an operating state; Responding to a call request of any thread to the target service, and acquiring the status bit; Judging whether the state bit indicates that the target service is in a stop state, if so, feeding back error call information, if not, increasing the counting bit by a first value, executing business logic corresponding to the target service, and reducing the counting bit by the first value after the business logic is executed; Updating 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 stop state, and executing exit logic corresponding to the target service under the condition that the count bit is cleared; The target service is used for providing services to the outside through an interface, the recorded atomic variable is a multi-bit binary variable, each bit of the recorded atomic variable comprises the counting bit and the state bit, the state bit is used for indicating that the current state of the target service is an operating state or a stopping state, the counting bit represents the number of objects currently served when the target service is in the operating state, and the atomic variable is used for ensuring that any thread cannot be interfered by other threads when operating the atomic variable.
- 2. The multi-threaded service exit method of claim 1 wherein the record atomic variable is a multi-bit integer type atomic variable, the lowest bit of the record atomic variable is the status bit, and the remaining bits of the record atomic variable are the count bits.
- 3. The multi-threaded service exit method of claim 2 wherein the obtaining the status bit comprises: reading the record atomic variable; And performing AND operation on the read result and 1 to obtain the state bit.
- 4. The multi-threaded service exit method of claim 2 wherein the first value is 2.
- 5. The multi-threaded service exit method of claim 4 wherein the status bit indicates that the target service is in the stopped state when the status bit is equal to 1 and indicates that the target service is in the running state when the status bit is equal to 0.
- 6. The method of multithreaded service exit of claim 1 wherein the incrementing the count bit by a first value comprises: the count bit is incremented by the first value using an atomic increment operation.
- 7. The multi-threaded service exit method of claim 1 wherein said decrementing said count bit by said first value comprises: The count bit is decremented by the first value using an atomic decrement operation.
- 8. A multi-threaded service exit apparatus, comprising: The initialization module is used for initializing the record atomic variable so as to enable the counting bit of the record atomic variable to be cleared and enable the state bit to represent that the target service is in an operation state; The first processing module is used for responding to a call request of any thread to the target service and acquiring the state bit; The second processing module is used for judging whether the state bit indicates that the target service is in a stop state, if so, feeding back error call information, if not, increasing the counting bit by a first value, executing business logic corresponding to the target service, and reducing the counting bit by the first value after the business logic is executed; the third processing module is used for responding to a stop service request aiming at the target service, updating the state bit so that the state bit indicates that the target service is in a stop state, and executing exit logic corresponding to the target service under the condition that the counting bit is cleared; The target service is used for providing services to the outside through an interface, the recorded atomic variable is a multi-bit binary variable, each bit of the recorded atomic variable comprises the counting bit and the state bit, the state bit is used for indicating that the current state of the target service is an operating state or a stopping state, the counting bit represents the number of objects currently served when the target service is in the operating state, and the atomic variable is used for ensuring that any thread cannot be interfered by other threads when operating the atomic variable.
- 9. A computer device comprising one or more processors and a memory having stored therein computer readable instructions which, when executed by the one or more processors, perform the steps of the multi-threaded service exit method of any of claims 1-7.
- 10. A storage medium having stored therein computer readable instructions which, when executed by one or more processors, cause the one or more processors to perform the steps of the multi-threaded service exit method of any of claims 1-7.
Description
Multithreading service exit method, device, storage medium and computer equipment Technical Field The present application relates to the field of computer technologies, and in particular, to a method and apparatus for exiting multithreading service, a storage medium, and a computer device. Background With the development of computer technology, many software programs provide corresponding services by way of an external open interface. Many software programs run in a multi-threaded environment, with other threads continuing to call an interface service when the interface service has stopped servicing. The logic that stops exiting the interface service and the business logic of the interface itself may overlap concurrently, causing the program to crash. The traditional scheme is to lock, but for interfaces frequently called, frequent locking and unlocking can affect the operation efficiency. Disclosure of Invention The present application aims to solve at least one of the above technical drawbacks, and in particular, the technical drawbacks of the prior art that the robustness test result is not accurate enough. In a first aspect, an embodiment of the present application provides a method for exiting multithreading service, including: initializing a record atom variable so as to enable a count bit of the record atom variable to be cleared and a state bit to represent that the target service is in an operating state; Responding to a call request of any thread to a target service, and acquiring a status bit; Judging whether the status bit indicates that the target service is in a stop state, if so, feeding back error call information, if not, increasing the counting bit by a first value, executing service logic corresponding to the target service, and reducing the counting bit by the first value after the service logic is executed; And in response to the stop service request aiming at the target service, updating the state bit to enable the state bit to represent that the target service is in a stop state, and executing the exit logic corresponding to the target service under the condition that the count bit is cleared. In one embodiment, the record atomic variable is a multi-bit integer type atomic variable, the lowest bit of the record atomic variable is a status bit, and the remaining bits of the record atomic variable are count bits. In one embodiment, the acquiring the status bit includes: Reading a record atom variable; and performing AND operation on the read result and 1 to obtain a status bit. In one embodiment, the first value is 2. 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 an operating state. In one embodiment, incrementing the count bit by a first value includes: the count bit is incremented by a first value using an atomic increment operation. In one embodiment, decrementing the count bit by a first value includes: the count bit is decremented by a first value using an atomic decrement operation. In a second aspect, an embodiment of the present application provides a multithreaded service exit apparatus, including: the initialization module is used for initializing the record atomic variable so as to zero the counting bit of the record atomic variable and enable the state bit to represent that the target service is in an operating state; The first processing module is used for responding to a call request of any thread to the target service and acquiring a status bit; The second processing module is used for judging whether the status bit indicates that the target service is in a stop state, if so, feeding back error calling information, if not, increasing the counting bit by a first numerical value, executing service logic corresponding to the target service, and reducing the counting bit by the first numerical value after the service logic is executed; And the third processing module is used for responding to the stop service request aiming at the target service, updating the state bit so as to enable the state bit to represent the target service to be in a stop state, and executing the exit logic corresponding to the target service under the condition that the counting bit is cleared. In a third aspect, embodiments of the present application provide a computer device comprising one or more processors and a memory having stored therein computer readable instructions which, when executed by the one or more processors, perform the steps of the multithreaded service exit method of any of the embodiments described above. In a fourth aspect, embodiments of the present application provide a storage medium having stored therein computer readable instructions which, when executed by one or more processors, cause the one or more processors to perform the steps of the multithreaded service exit method of any of the embodiments desc