A Singleton Processing Method and Apparatus
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-15
- Publication Date
- 2026-08-14
AI Technical Summary
[0003]有鉴于此,本发明实施例提供一种单例处理方法和装置,至少能够解决现有技术中单例频繁使用造成的线程死锁、不支持释放问题
[0035]根据本发明所述提供的方案,上述发明中的一个实施例具有如下优点或有益效果:只有单例管理者的单例是使用dispatch_once方式创建的,其他单例都是通过单例管理者的创建方法创建,不再使用dispatch_once方式,解决了现有创建单例均使用dispatch_once方式导致的线程锁死问题;全局只有一份创建单例的代码,既能减少代码量,又能保证创建单例的代码统一;提供了移除单例的程序,释放后下次使用依旧会重新创建,使开发者使用更加方便快捷。
Smart Images

Figure CN114741209B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a singleton processing method and apparatus. Background Technology
[0002] There are many ways to create singleton instances, such as single-threaded singletons, synchronized singletons, and dispatch_once singletons, but most use the dispatch_once method. However, in practice, some threads may call each other, which can lead to thread deadlock. Singletons created in this way can only be created once in the entire thread. If they are released, they cannot be recreated during the entire thread's lifetime. Therefore, singletons created in the current way do not support release. Summary of the Invention
[0003] In view of this, embodiments of the present invention provide a singleton processing method and apparatus, which can at least solve the problems of thread deadlock and lack of release support caused by frequent use of singletons in the prior art.
[0004] To achieve the above objectives, according to one aspect of the present invention, a singleton processing method is provided, comprising:
[0005] In response to a call to the program that retrieves the singleton, the system queries whether the corresponding singleton object exists in the singleton manager based on the class name in the call.
[0006] If the query result indicates that the singleton object exists, then the singleton object will be returned as the result of the call operation; or
[0007] If the query result does not exist, a locking mechanism is used to call the singleton creation procedure of the singleton manager to create a singleton object using the class name. The created singleton object is returned as the result of the call operation, and the class name and the created singleton object are stored in the singleton manager as key-value pairs.
[0008] Optionally, the method of creating a singleton object using the class name with locking includes:
[0009] Locking is applied to the code in the singleton manager used to create singleton objects, so that a multi-threaded program can only create singleton objects through one thread at a time.
[0010] The code is invoked based on a single thread to create a singleton object using the class name.
[0011] Optionally, prior to the call operation to the procedure for obtaining the singleton, the method further includes:
[0012] Create a singleton instance as the singleton manager, which receives configuration commands from the program that retrieves the singleton instance; and
[0013] Add the procedure for obtaining the singleton to all classes.
[0014] Optionally, the method also includes configuring a procedure for removing the singleton, and further includes:
[0015] The system receives the class name to be removed and, in response to the existence of a singleton object corresponding to the type to be removed in the singleton manager, calls the program to remove the singleton and remove the singleton object and the class name to be removed using a locking mechanism.
[0016] Optionally, the method also includes configuring a procedure to remove all singletons, and further includes:
[0017] In response to the call to the procedure for removing all singletons, a locking mechanism is used to remove all singleton objects and class names in the singleton manager.
[0018] To achieve the above objectives, according to another aspect of the present invention, a singleton processing apparatus is provided, comprising:
[0019] The query module is used to respond to the call operation of the program to obtain the singleton, and to query whether the corresponding singleton object exists in the singleton manager based on the class name in the call operation;
[0020] The first processing module is used to return the singleton object as the result of the call operation if the query result is that it exists.
[0021] The second processing module is used to call the singleton creation program of the singleton manager in a locking manner if the query result does not exist, so as to create a singleton object by means of the class name, return the created singleton object as the result of the call operation, and store the class name and the created singleton object in the form of key-value pairs in the singleton manager.
[0022] Optionally, the second processing module is used for:
[0023] Locking is applied to the code in the singleton manager used to create singleton objects, so that a multi-threaded program can only create singleton objects through one thread at a time.
[0024] The code is invoked based on a single thread to create a singleton object using the class name.
[0025] Optionally, a configuration module is also included for:
[0026] Create a singleton instance as the singleton manager, which receives configuration commands from the program that retrieves the singleton instance; and
[0027] Add the procedure for obtaining the singleton to all classes.
[0028] Optionally, the device also includes a procedure for configuring the removal of singletons, and further includes a partial singleton removal module for:
[0029] The system receives the class name to be removed and, in response to the existence of a singleton object corresponding to the type to be removed in the singleton manager, calls the program to remove the singleton and remove the singleton object and the class name to be removed using a locking mechanism.
[0030] Optionally, the device also includes a procedure for configuring the removal of all singletons, and further includes a singleton removal module for:
[0031] In response to the call to the procedure for removing all singletons, a locking mechanism is used to remove all singleton objects and class names in the singleton manager.
[0032] To achieve the above objectives, according to another aspect of the present invention, a singleton management electronic device is provided.
[0033] An electronic device according to an embodiment of the present invention includes: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement any of the singleton processing methods described above.
[0034] To achieve the above objectives, according to another aspect of the present invention, a computer-readable medium is provided having a computer program stored thereon, which, when executed by a processor, implements any of the singleton processing methods described above.
[0035] According to the solution provided by the present invention, one embodiment of the above invention has the following advantages or beneficial effects: only the singleton manager's singleton is created using the dispatch_once method, while other singletons are created through the singleton manager's creation method, no longer using the dispatch_once method, thus solving the thread deadlock problem caused by the existing use of the dispatch_once method for creating singletons; there is only one copy of the code for creating singletons globally, which can reduce the amount of code and ensure the uniformity of the code for creating singletons; a procedure for removing singletons is provided, and the singleton will be recreated the next time it is used after being released, making it more convenient and faster for developers to use.
[0036] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description
[0037] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein:
[0038] Figure 1 This is a schematic diagram of the main flow of a singleton processing method according to an embodiment of the present invention;
[0039] Figure 2 This is a flowchart illustrating a specific singleton processing method according to an embodiment of the present invention;
[0040] Figure 3 This is a schematic diagram of the main modules of a singleton processing device according to an embodiment of the present invention;
[0041] Figure 4 This is an exemplary system architecture diagram in which embodiments of the present invention can be applied;
[0042] Figure 5 This is a schematic diagram of the structure of a computer system suitable for implementing the embodiments of the present invention, such as a mobile device or server. Detailed Implementation
[0043] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.
[0044] There are several ways to create a singleton instance, such as single-threaded, synchronized, and dispatch_once. Single-threaded methods require ensuring only one instance exists, so the instance is created only on the first access to it, and subsequent accesses retrieve the already created instance. With synchronized methods, if two threads simultaneously call shareInstance (the program retrieving the singleton), two singletons might be created. Therefore, in multi-threaded scenarios, locking is necessary.
[0045] While `synchronized` solves the multithreading problem to some extent, it's not perfect. Locking is only necessary when the singleton hasn't been created. If the singleton has already been created, locking will impact performance. Compared to single-threaded implementations and `synchronized`, `dispatch_once` is convenient and efficient, therefore it's currently the most common method for creating singletons.
[0046] The `dispatch_once` process works as follows: 1) When thread A executes its block, other threads must wait. After thread A finishes executing its block, it should immediately mark its task as complete and then traverse the semaphore chain to wake up all waiting threads. While thread A is traversing the semaphore chain `signal`, any other newly entered thread should return immediately without waiting. If another waiting thread B is still updating or attempting to update the semaphore chain, it should be ensured that thread B can correctly complete its task: a. return immediately, b. wait on the semaphore and be quickly woken up again.
[0047] 2) When thread B constructs the semaphore list, it should consider that thread A may change its state at any time (waiting, completing, traversing the semaphore list). It should also consider that another thread C may be updating or attempting to update the semaphore list. It should be ensured that both B and C can complete their tasks normally: a. add a link and wait on the semaphore, b. find that thread A has marked "complete" and then directly destroy the semaphore and exit the function.
[0048] When two classes call each other's singleton methods, the caller TestA acts as a waiter, waiting for the block in TestB to complete. The completion of TestB's block depends on the completion of TestA's singleton function's block, and TestA's block requires TestB's block to complete as well... Both are waiting for each other to finish, creating a deadlock. If, during the execution of a block in the dispatch_once function, the caller loops into its own dispatch_once function, the linked list will grow indefinitely, also causing a deadlock. This can occur with loops like A->B->A->B->A, or even more direct loops like A->A->A. For complex chained loops like A->B->C->A->B->C->A, it's difficult to intuitively determine if a loop exists.
[0049] This solution first creates a singleton instance, `SingleInstanceManager`, as a management tool (singleton manager). Only this singleton is created using the `dispatch_once` method; all other singletons are created using `SingleInstanceManager`'s creation method (by class name), eliminating the need for `dispatch_once`. This resolves the thread deadlock issue caused by the current practice of using `dispatch_once` for all singleton creation. Furthermore, developers can use the singleton creation method within `SingleInstanceManager` to generate singletons based on classes, reducing code size and ensuring consistent code for singleton creation.
[0050] See Figure 1 The diagram shows the main flowchart of a singleton processing method provided by an embodiment of the present invention, which includes the following steps:
[0051] S101: In response to the call operation of the program to obtain the singleton, query whether the corresponding singleton object exists in the singleton manager based on the class name in the call operation;
[0052] S102: If the query result is that the singleton object exists, then the singleton object will be returned as the result of the call operation;
[0053] S103: If the query result is not found, the singleton manager's singleton creation procedure is called using a locking method to create a singleton object with the class name, the created singleton object is returned as the result of the call operation, and the class name and the created singleton object are stored in the singleton manager in the form of key-value pairs.
[0054] In the above implementation, for steps S101 to S103, this solution adopts the Singleton pattern. The Singleton pattern belongs to the creational pattern and provides an optimal way to create objects. This pattern involves a single class that is responsible for creating its own objects, while ensuring that only a single object is created. Moreover, this class provides a way to access its unique object directly without needing to instantiate an object of the class.
[0055] First, a singleton instance, `SingleInstanceManager`, is created as a management tool (singleton manager) for singleton objects. Three callers are declared: `shareInstance` for retrieving singletons, `removeInstance` for removing a specific singleton, and `clearManagers` for removing all singletons. By creating categories based on the `NSObject` base class, a default singleton retrieval method, `shareInstance`, is added to all classes developed by the developer for convenience.
[0056] When a developer calls `-(NSObject*)getDictObjectInstanceWithClassStr:(NSString*)classStr` to retrieve a singleton instance, this can be used as the trigger condition for locally querying the singleton. Internally, it first checks if the corresponding singleton object exists in `SingleInstanceManager` using the class name `classStr`. If it does, the singleton object is returned directly to the caller; otherwise, it is created using a locking mechanism. `SingleInstanceManager` internally maintains a mapping table (or member variable dictionary) that includes the mapping relationship between class names and singleton objects. The singleton object is retrieved using its class name.
[0057] After creation, the created singleton object and class name are stored as key-value pairs in the member variable dictionary of SingleInstanceManager, which is also a mapping table. Subsequent attempts to retrieve the singleton object using getDictObjectInstanceWithClassStr can directly find it in SingleInstanceManager. This type of object, which exists only once globally in memory, is a singleton object; that is, a singleton has been created.
[0058] Furthermore, since the newly created singleton object is stored in the SingleInstanceManager mapping table, it has the same lifecycle as SingleInstanceManager. Moreover, it is created using the runtime feature NSClassFromString, which locks the thread during the write process to ensure thread safety and prevent thread deadlocks caused by the misuse of dispatch_once. The thread lock ensures that only one thread accesses the shared resource at any given time, avoiding the problem of shared data corruption caused by multiple threads. The purpose of NSClassFromString is to check if the class name exists; if it exists, it dynamically loads and creates a singleton object of that class; otherwise, it returns an empty object.
[0059] Developers can retrieve the singleton object by calling `shareInstance` using the class name. If the query result is non-existent, this solution will create a copy of the singleton object and store it in `SingleInstanceManager`, returning it as the return value to the caller. The entire application only creates this singleton object during initialization; subsequent calls using the class name will only retrieve the first created singleton object and will not recreate it.
[0060] This solution uses locking to create singleton objects using the class name string. Locking transforms concurrent execution into sequential execution, ensuring that only one singleton object is created at a time. Specifically, the code used to create the singleton object is locked, allowing only one thread to create the singleton object at a time in a multi-threaded program. Then, a single thread calls this code to create the singleton object using the class name.
[0061] The above embodiments provide developers with a singleton handling method, which obtains a singleton object of any class through shareInstance. This singleton object has one and only one copy in the entire application. Even if it does not exist, the singleton creation program of the singleton manager will be called to generate it based on the class.
[0062] See Figure 2 The diagram illustrates a specific singleton processing method according to an embodiment of the present invention, including the following steps:
[0063] S201: Create a singleton as a singleton manager, receive configuration operations for the procedure that retrieves the singleton, and add the procedure for retrieving the singleton to all classes;
[0064] S202: In response to the call operation of the procedure to obtain the singleton, query whether the corresponding singleton object exists in the singleton manager based on the class name in the call operation;
[0065] S203: If the query result is that the singleton object exists, then the singleton object will be returned as the result of the call operation;
[0066] S204: If the query result is not found, lock the code in the singleton manager used to create the singleton object so that a multi-threaded program can only create the singleton object through one thread at a time.
[0067] S205: The code is called based on the aforementioned thread to create a singleton object using the aforementioned class name;
[0068] S206: Return the created singleton object as the result of the call operation, and store the class name and the created singleton object in the form of key-value pairs to the singleton manager.
[0069] The above only describes the procedure for obtaining the singleton; the remaining two procedures are described here:
[0070] 1. Remove a singleton instance using `removeInstance`: This function removes only one singleton object. It receives the class name to be removed, checks if a singleton object exists in the singleton manager, and if it does, calls `removeInstance` to remove both the singleton object and the class name using a locking mechanism.
[0071] 2. The `clearManagers` function removes all singleton instances. In response to a call to remove all singleton instances, it uses locking to remove all singleton objects and their class names from the singleton manager.
[0072] Currently, most singletons in iOS development are created using the `dispatch_once` method. Singletons created using `dispatch_once` can only be created once within the entire thread; if released, they cannot be recreated during the thread's lifetime. Therefore, singletons created using this method do not support release. This solution uses the methods provided by the singleton manager to create singletons, instead of using the `dispatch_once` method, and is therefore unaffected by it. Furthermore, the singleton manager provides methods for releasing a singleton or all singletons; after release, they will still be recreated the next time they are used, making it more flexible.
[0073] It's important to note that iOS development uses Objective-C, a language with the following two characteristics: 1. The NSObjet object is the base class in iOS development; all objects used in development inherit from NSObjet. 2. Objective-C allows adding categories to objects, enabling the addition of methods beyond those already provided by the system. Based on these characteristics, a category called NSObject+ShareInstance was designed to be added to NSObject objects, implementing the +(instancetype)shareInstance method. This method allows any object inheriting from NSObjet to call shareInstance to retrieve the singleton object.
[0074] The method provided in the above embodiments has at least the following advantages over the prior art:
[0075] 1. The singleton that acts as the singleton manager is created using the dispatch_once method, while other singletons are created through the singleton manager's creation method without needing to use the dispatch_once method. This solves the thread deadlock problem caused by the existing use of the dispatch_once method for creating singletons and simplifies the operation.
[0076] 2. There is only one copy of the code for creating the singleton globally, which reduces the amount of code, reduces redundant code, and ensures the consistency of the code for creating the singleton.
[0077] 3. Provides a program to remove a singleton and a program to remove all singletons. After release, they will be recreated the next time they are used, making it more convenient and faster for developers to operate singletons.
[0078] See Figure 3 The diagram shows a schematic of the main modules of a singleton processing device 300 provided in an embodiment of the present invention, including:
[0079] The query module 301 is used to respond to the call operation of the program to obtain the singleton, and to query whether the corresponding singleton object exists in the singleton manager according to the class name in the call operation;
[0080] The first processing module 302 is used to return the singleton object as the result of the call operation if the query result is that it exists.
[0081] The second processing module 303 is used to, if the query result does not exist, create a singleton object using the class name by employing locking, return the created singleton object as the result of the call operation, and store the class name and the created singleton object in the form of key-value pairs in the singleton manager. It also performs locking on the code used to create the singleton object so that a multi-threaded program can only create the singleton object through one thread at a time; and calls the code based on this single thread to create the singleton object using the class name.
[0082] The apparatus of the present invention further includes a configuration module, configured to: create a singleton as a singleton manager, receive configuration operations for the program that retrieves the singleton; and add the program that retrieves the singleton to all classes.
[0083] The apparatus for implementing the present invention further includes a program for configuring the removal of singletons, and the apparatus further includes a partial singleton removal module for:
[0084] The system receives the class name to be removed and, in response to the existence of a singleton object corresponding to the type to be removed in the singleton manager, calls the program to remove the singleton and remove the singleton object and the class name to be removed using a locking mechanism.
[0085] The apparatus for implementing the present invention further includes a program for configuring the removal of all singletons, and the apparatus further includes a module for removing all singletons, used for:
[0086] In response to the call to the procedure for removing all singletons, a locking mechanism is used to remove all singleton objects and class names in the singleton manager.
[0087] Furthermore, the specific implementation details of the device described in the embodiments of the present invention have been described in detail in the above-described method, so the details will not be repeated here.
[0088] Figure 4 An exemplary system architecture 400 to which embodiments of the present invention can be applied is shown, including terminal devices 401, 402, 403, network 404, and server 405 (this is merely an example).
[0089] Terminal devices 401, 402, and 403 can be various electronic devices with displays and support for web browsing, and have various communication client applications installed. Users can use terminal devices 401, 402, and 403 to interact with server 405 through network 404 to receive or send messages, etc.
[0090] Network 404 is a medium used to provide a communication link between terminal devices 401, 402, 403 and server 405. Network 404 may include various connection types, such as wired or wireless communication links or fiber optic cables, etc.
[0091] Server 405 can be a server that provides various services. It should be noted that the methods provided in the embodiments of the present invention are generally executed by server 405, and correspondingly, the devices are generally set in server 405.
[0092] It should be understood that Figure 4 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0093] The following is for reference. Figure 5 It shows a schematic diagram of the structure of a computer system 500 suitable for implementing a terminal device of the present invention. Figure 5 The terminal device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of the present invention.
[0094] like Figure 5 As shown, the computer system 500 includes a central processing unit (CPU) 501, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 502 or programs loaded from storage section 508 into random access memory (RAM) 503. The RAM 503 also stores various programs and data required for the operation of the system 500. The CPU 501, ROM 502, and RAM 503 are interconnected via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0095] The following components are connected to I / O interface 505: an input section 506 including a keyboard, mouse, etc.; an output section 507 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 508 including a hard disk, etc.; and a communication section 509 including a network interface card such as a LAN card, modem, etc. The communication section 509 performs communication processing via a network such as the Internet. A drive 510 is also connected to I / O interface 505 as needed. A removable medium 511, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 510 as needed so that computer programs read from it can be installed into storage section 508 as needed.
[0096] In particular, according to the embodiments disclosed in this invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 509, and / or installed from removable medium 511. When the computer program is executed by central processing unit (CPU) 501, it performs the functions defined above in the system of this invention.
[0097] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media can also be any computer-readable medium other than computer-readable storage media, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.
[0098] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0099] The modules described in the embodiments of the present invention can be implemented in software or hardware. The described modules can also be located in a processor; for example, a processor can be described as including a query module, a processing module, and a storage module. The names of these modules do not necessarily limit the module itself; for example, a storage module can also be described as a "return and store module".
[0100] In another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs that, when executed by the device, cause the device to perform a singleton processing method.
[0101] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.
Claims
1. A singleton handling method, characterized in that, include: Create a singleton as the singleton manager; only the singleton manager is created using the dispatch_once method, and all other singletons are created through the singleton creation procedure of the singleton manager, without using the dispatch_once method. In response to a call to the program that retrieves the singleton, the system queries whether the corresponding singleton object exists in the singleton manager based on the class name in the call. If the query result is that the singleton object exists, then the singleton object will be returned as the result of the call operation. If the query result does not exist, a locking mechanism is used to call the singleton creation procedure of the singleton manager to create a singleton object using the class name. The created singleton object is then returned as the result of the call operation, and the class name and the created singleton object are stored in the singleton manager as key-value pairs.
2. The method according to claim 1, characterized in that, The method of using locking to call the singleton manager's singleton creation procedure to create a singleton object using the class name includes: Locking is applied to the code in the singleton manager used to create singleton objects, so that a multi-threaded program can only create singleton objects through one thread at a time. The code is invoked based on a single thread to create a singleton object using the class name.
3. The method according to claim 1, characterized in that, Prior to the call operation to the procedure for obtaining the singleton, the method also includes: Receive configuration operations for the program that retrieves the singleton; and Add the procedure for obtaining the singleton to all classes.
4. The method according to claim 3, characterized in that, It also includes a procedure for configuring the removal of a singleton, and the method further includes: The system receives the class name to be removed and, in response to the existence of a singleton object corresponding to the class name to be removed in the singleton manager, calls the program to remove the singleton and remove the singleton object and the class name to be removed using a locking mechanism.
5. The method according to claim 3, characterized in that, It also includes a procedure for configuring the removal of all singletons, and the method further includes: In response to the call to the procedure for removing all singletons, a locking mechanism is used to remove all singleton objects and class names in the singleton manager.
6. A singleton processing device, characterized in that, include: The configuration module is used to create a singleton as a singleton manager; only the singleton manager is created using the dispatch_once method, and other singletons are created through the singleton creation procedure of the singleton manager, without using the dispatch_once method. The query module is used to respond to the call operation of the program to obtain the singleton, and to query whether the corresponding singleton object exists in the singleton manager based on the class name in the call operation; The first processing module is used to return the singleton object as the result of the call operation if the query result is that it exists. The second processing module is used to call the singleton creation program of the singleton manager in a locking manner if the query result does not exist, so as to create a singleton object by means of the class name, return the created singleton object as the result of the call operation, and store the class name and the created singleton object in the form of key-value pairs in the singleton manager.
7. The apparatus according to claim 6, characterized in that, The second processing module is used for: Locking is applied to the code in the singleton manager used to create singleton objects, so that a multi-threaded program can only create singleton objects through one thread at a time. The code is invoked based on a single thread to create a singleton object using the class name.
8. The apparatus according to claim 6, characterized in that, The configuration module is also used for: Declare the procedure for obtaining the singleton; and Add the procedure for obtaining the singleton to all classes.
9. An electronic device, characterized in that, include: One or more processors; Storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1-5.
10. A computer-readable medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-5.
Citation Information
Patent Citations
Singleton class interface calling method, terminal device and storage medium
CN111240651A