- Notifications
You must be signed in to change notification settings - Fork4
C++ implementation of an object pool pattern
License
massimo-marino/object-pool
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
C++17 implementation of an object pool pattern
This code is a fork & extension from the following code:
http://media.wiley.com/product_ancillary/50/11188580/DOWNLOAD/c25_code.zip
From the original source file:
--- cit ON
Provides an object pool that can be used with any class that provides a default constructor.
The object pool constructor creates a pool of objects, which it hands outto clients when requested via the acquireObject() method. acquireObject()returns an Object which is a std::shared_ptr with a custom deleter thatautomatically puts the object back into the object pool when the shared_ptris destroyed and its reference reaches 0.
The constructor and destructor on each object in the pool will be called onlyonce each for the lifetime of the program, not once per acquisition and release.
The primary use of an object pool is to avoid creating and deleting objectsrepeatedly. The object pool is most suited to applications that use largenumbers of objects with expensive constructors for short periods of time, ifa profiler tells you that allocating and deallocating these objects is abottleneck.
--- cit OFF
It is also possible to create an object pool providing a non-default ctor forthe objects that are registered and used at any new allocation when the poolis empty
Objects returned to the pool are reset by default.
See:https://en.wikipedia.org/wiki/Object_pool_pattern
See the unit tests for examples of use.
cmake
is used to compile the sources.
The default compiler used isclang++
.
The cmake files compile with-std=c++20
.
The unit tests are implemented ingoogletest
: be sure you have installedgoogletest
to compile.
$ git clone https://github.com/massimo-marino/object-pool.git$cd object-pool$ mkdir build$cd build$ cmake ..$ make
Unit tests are implemented withgoogletest
.
Installgoogletest
to compile and run them.
$cd src/unitTests$ ./unitTests
$cd ../example$ ./object-pool-example