|
VLink 2.0.0
A high-performance communication middleware
|
Thread-safe generic object pool with configurable reset policy and RAII ownership. 更多...
#include <functional>#include <memory>#include <mutex>#include <sstream>#include <stdexcept>#include <utility>#include <vector>#include "./macros.h"类 | |
| class | vlink::ObjectPool< T > |
Thread-safe object pool for type T with RAII acquisition and configurable reset policy. 更多... | |
| struct | vlink::ObjectPool< T >::Stats |
| Snapshot of pool statistics at a point in time. 更多... | |
| struct | vlink::ObjectPool< T >::PoolDeleter |
Custom deleter for RAII handles returned by get() and get_shared(). 更多... | |
命名空间 | |
| namespace | vlink |
Thread-safe generic object pool with configurable reset policy and RAII ownership.
ObjectPool<T> maintains a free-list of pre-allocated T objects. Callers acquire an object, use it, then return it to the pool automatically (RAII) or manually. Objects are recycled rather than destroyed, reducing heap pressure in hot paths.
Acquisition API:
| Method | Return type | Auto-return on destruction |
|---|---|---|
get() | unique_ptr<T, PoolDeleter> | Yes (via PoolDeleter) |
get_shared | shared_ptr<T> (PoolDeleter) | Yes (via shared_ptr deleter) |
borrow() | T* (raw pointer) | No – caller must call give_back() |
Reset policy controls when the optional ResetCallback is invoked:
| Policy | Reset on acquire | Reset on release | Use case |
|---|---|---|---|
kPolicyNone | No | No | Immutable / stateless objects |
kPolicyRelease | No | Yes | Clean before returning (default) |
kPolicyAcquire | Yes | No | Clean before use |
kPolicyBoth | Yes | Yes | Clean on both sides |
max_size is 0 (default), the pool grows without bound.max_size > 0 and the number of live objects reaches max_size), get(), get_shared(), and borrow() throw std::runtime_error.FactoryCallback returns nullptr, a std::runtime_error is thrown.| T | Type of objects managed by the pool. |