1. What is an object pool?
Pools (Poo), which are somewhat similar to collections in some sense. A pool, which is a set of quantities of water; a pool of memory is a set of allocated memory, and a thread pool is a collection of a number of threads that have been created. Then, the object pool, as the name implies, is a set of a number of objects that have been created (object).
2. What is the object pool for?
For example, in the case of a pool of life, when there is no pool, every time water, you have to go to a very far place to carry water, after the pool, a lot of time to pick up, put in the pool to store, so after a while, water when you do not have to pick up, directly from the pool to take. In the same way, in a C + + program, if an object, you often use Malloc/free (or new/delete) to create, destroy, on the one hand, the cost will be relatively large, on the other hand will produce a lot of memory fragmentation, the program runs for a long time, performance will fall. This time, the object pool is created. You can create a batch of objects in advance, put them in a collection, and then retrieve them from the object pool whenever the program needs new objects, and return the object to the object pool whenever the program finishes using the object. In this way, there will be a lot less malloc/free (new/delete) calls, to a certain extent, improve the performance of the system, especially in the dynamic memory allocation more frequent programs in the effect is more obvious.
3. What are the characteristics of the object pool?
In general, the object pool has several characteristics:
(1) A certain number of objects have been created in the object pool
(2) The object pool provides the user with an interface to get the object, and when the user needs a new object, the new object can be obtained by calling this interface. If there is a pre-created object in the object pool, it is returned directly to the user, and if not, the object pool can also create a new object to join it and return it to the user
(3) The object pool provides an interface to the user to return the object, which can be returned to the object pool by this interface when the user no longer uses an object
4. How is the object pool implemented?
Just provide two interfaces to simulate with a queue.
Object Pool Mode