Generic resource pool utility class. More...
#include <resourcePool.hpp>
Public Member Functions | |
| ResourcePool (const C &config, std::size_t maxSize, std::size_t initialSize=0, std::chrono::milliseconds timeout=std::chrono::milliseconds(100)) | |
| ResourcePool (const ResourcePool &)=delete | |
| ResourcePool (ResourcePool &&)=delete | |
| ~ResourcePool () | |
| ResourcePool & | operator= (const ResourcePool &)=delete |
| ResourcePool & | operator= (ResourcePool &&)=delete |
| anch::PoolableResource< T > | borrowResource () |
| void | setTimeout (std::chrono::milliseconds timeout) |
Generic resource pool utility class.
Maximum number of resources is mandatory.
Intialize pool size will instanciate resources for imediate use.
Resources will be instanciated with their configuration, outside the pool lock.
If no resource is available, pool will wait until timeout (default to 100ms) is reached.
Waiting borrowers are served in arrival order (FIFO): a borrower can not be overtaken by later ones, so waiting times stay bounded under contention.
Resources are automatically released through their destructor: release() is called on resources which implement it (Releasable), then invalid resources are destroyed and will be replaced on demand.
If you have only one action to do, you can use pool.borrowResource().get().doAction() ; otherwise you should to keep anch::PoolableResource until all actions have been done (with auto res = pool.borrowResource()).
Borrowed resources can outlive their pool: they will be destroyed when released.
You can specifiy an std::shared_ptr creation function as third template parameter. By default, std::make_shared<T> will be used. It can be usefull for polymorphism dynamic allocation.
| anch::ResourcePool< T, C, make_ptr >::ResourcePool | ( | const C & | config, |
| std::size_t | maxSize, | ||
| std::size_t | initialSize = 0, | ||
| std::chrono::milliseconds | timeout = std::chrono::milliseconds(100) ) |
ResourcePool constructor
Resources which can not be created at initialization will be created on demand.
| config | the resource configuration |
| maxSize | the pool maximum size |
| initialSize | the initialize size |
| timeout | the timeout |
| std::invalid_argument | when maxSize is 0 |
|
delete |
Prohibits ResourcePool copy constructor
|
delete |
Prohibits ResourcePool move constructor
| anch::ResourcePool< T, C, make_ptr >::~ResourcePool | ( | ) |
ResourcePool destructor
Destroy every available resource. Borrowed resources will be destroyed when released.
| anch::PoolableResource< T > anch::ResourcePool< T, C, make_ptr >::borrowResource | ( | ) |
Retrieve available resource if there is one.
Create a new resource if maximum size has not been reached.
This method will raise TimeoutException if timeout has been reached while waiting available resource or any other exception thrown by the resource constructor.
anch::PoolableResource)
|
delete |
Prohibits ResourcePool copy assignment
|
delete |
Prohibits ResourcePool move assignment
| void anch::ResourcePool< T, C, make_ptr >::setTimeout | ( | std::chrono::milliseconds | timeout | ) |
Timeout setter
| timeout | the timeout to set |