arrp 0.0.15
Auto returning resource pool for modern C++
Loading...
Searching...
No Matches
siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity > Class Template Reference

Thread-safe auto-returning resource pool for modern C++. More...

#include <siddiqsoft/resource_pool.hpp>

Public Types

enum class  auto_add_policy { NoGrow , AutoGrow }
 This controls the auto-grow (or adding items when the pool is starving) and below capacity (up to the maximum limit). The load is calculated as. More...

Public Member Functions

 resource_pool (std::function< SRT(resource_pool &)> &&new_resource_callback)
 resource_pool (auto_add_policy add_policy=auto_add_policy::NoGrow)
 This is the default constructor.. the policy is to not auto-grow. The client code can as for AutoGrow in which case we will use the lambda to get the derived-class to build its custom pool.
 resource_pool (resource_pool &)=delete
 Copy constructor is deleted.
 resource_pool (resource_pool &&src)=delete
 Move constructor is deleted.
resource_pool & operator= (resource_pool &)=delete
 Copy assignment operator is deleted.
resource_pool & operator= (resource_pool &&src)=delete
 Move assignment operator is deleted.
 ~resource_pool ()
 Destructor - clears all resources from the pool.
void clear () noexcept
size_t size () const noexcept
auto checkout () -> SRT
void checkin (T &&raw_resource)

Static Public Attributes

static std::function< SRT(resource_pool &)> CallbackDoNotAutoAddResource
 This callback is the default and does not grow the resource; it throws a runtime_error.

Detailed Description

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
requires ((InitCapacity <= resource_pool_limits::MaxCapacity)) && NonNumericMoveConstructible<T> && std::derived_from<SRT, scoped_resource<T>>
class siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >

Thread-safe auto-returning resource pool for modern C++.

Template Parameters
TThe resource type to be pooled. Must be move-constructible and non-arithmetic.
SRTThe scoped resource wrapper type. Defaults to scoped_resource<T>.
InitCapacityThe initial capacity of the pool. Defaults to resource_pool_limits::DefaultCapacity.

The resource_pool class implements a thread-safe object pool pattern with automatic resource management using RAII principles. Resources are automatically returned to the pool when the scoped_resource wrapper goes out of scope, eliminating manual resource management and reducing the risk of resource leaks.

Key Features:

  • Thread-Safe: All operations are protected by mutexes for concurrent access
  • RAII Pattern: Resources are automatically returned via scoped_resource destructors
  • Capacity Management: Enforces a maximum capacity limit to prevent unbounded growth
  • FIFO Ordering: Resources are retrieved from the front and added to the back
  • Customizable Factory: Supports custom resource creation callbacks
  • Diagnostic Counters: Tracks borrow, return, and auto-add operations
  • JSON Serialization: Provides pool state diagnostics via JSON (when nlohmann/json is available)

Usage Example:

// Create a pool with custom resource factory
auto pool = resource_pool<MyResource>(
[](resource_pool& p) -> scoped_resource<MyResource> {
MyResource{},
[&p](MyResource&& res) { p.checkin(std::move(res)); }
);
}
);
// Borrow a resource from the pool
{
auto resource = pool.checkout();
// Use the resource...
// Resource is automatically returned when it goes out of scope
}
RAII wrapper for managing resource lifecycle with automatic return to pool.

Thread Safety:

  • All public methods are thread-safe
  • Multiple threads can safely borrow and return resources concurrently
  • The pool uses a standard mutex for optimal performance

Constraints:

  • Not copy-constructible or move-constructible
  • Not copy-assignable or move-assignable
  • Resources must be move-constructible and non-arithmetic types
  • InitCapacity must not exceed resource_pool_limits::MaxCapacity
  • Capacity is limited to 255 resources (uint8_t)
Warning
Factory callbacks MUST NOT call any methods on the pool (checkout, checkin, clear, etc.) as this will cause deadlock. The callback should only create and return a new resource.
Note
The pool does not own resources directly; it manages scoped_resource wrappers
Resources are stored in a FIFO deque for predictable ordering
The pool is designed for long-lived objects that are expensive to create
Counters use uint64_t and will wrap around after ~18 quintillion operations
Examples
/opt/azure-agent/_work/20/s/include/siddiqsoft/resource_pool.hpp.

Definition at line 123 of file resource_pool.hpp.

Member Enumeration Documentation

◆ auto_add_policy

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
enum class siddiqsoft::arrp::resource_pool::auto_add_policy
strong

This controls the auto-grow (or adding items when the pool is starving) and below capacity (up to the maximum limit). The load is calculated as.

Examples
/opt/azure-agent/_work/20/s/include/siddiqsoft/resource_pool.hpp.

Definition at line 179 of file resource_pool.hpp.

Constructor & Destructor Documentation

◆ resource_pool() [1/2]

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >::resource_pool ( std::function< SRT(resource_pool< T, SRT, InitCapacity > &)> && new_resource_callback)
inline

Definition at line 223 of file resource_pool.hpp.

◆ resource_pool() [2/2]

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >::resource_pool ( auto_add_policy add_policy = auto_add_policy::NoGrow)
inline

This is the default constructor.. the policy is to not auto-grow. The client code can as for AutoGrow in which case we will use the lambda to get the derived-class to build its custom pool.

Definition at line 237 of file resource_pool.hpp.

References CallbackDoNotAutoAddResource.

◆ ~resource_pool()

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >::~resource_pool ( )
inline

Destructor - clears all resources from the pool.

Invokes the pool shutdown callback if registered, then clears all remaining resources from the pool. Any resources that are currently checked out are NOT affected by this operation.

Note
Safe to call even if the pool is empty
All remaining pooled resources are destroyed
Checked-out resources will be returned to the pool when their scoped_resource wrappers are destroyed
Examples
/opt/azure-agent/_work/20/s/include/siddiqsoft/resource_pool.hpp.

Definition at line 285 of file resource_pool.hpp.

Member Function Documentation

◆ checkin()

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
void siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >::checkin ( T && raw_resource)
inline

Definition at line 486 of file resource_pool.hpp.

◆ checkout()

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
auto siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >::checkout ( ) -> SRT
inlinenodiscard

Definition at line 377 of file resource_pool.hpp.

◆ clear()

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
void siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >::clear ( )
inlinenoexcept

Definition at line 305 of file resource_pool.hpp.

◆ size()

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
size_t siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >::size ( ) const
inlinenodiscardnoexcept

Definition at line 334 of file resource_pool.hpp.

Member Data Documentation

◆ CallbackDoNotAutoAddResource

template<typename T, typename SRT = scoped_resource<T>, uint8_t InitCapacity = resource_pool_limits::DefaultCapacity>
std::function<SRT(resource_pool&)> siddiqsoft::arrp::resource_pool< T, SRT, InitCapacity >::CallbackDoNotAutoAddResource
inlinestatic
Initial value:
= [](resource_pool&) -> SRT {
throw std::runtime_error("No items in the pool; add something first.");
}

This callback is the default and does not grow the resource; it throws a runtime_error.

Examples
/opt/azure-agent/_work/20/s/include/siddiqsoft/resource_pool.hpp.

Definition at line 186 of file resource_pool.hpp.

Referenced by resource_pool().


The documentation for this class was generated from the following file: