ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
|
Manager to control the lifetime of shared resources. More...
#include <ResourceManager.hpp>
Public Member Functions | |
ResourceManager ()=default | |
~ResourceManager () | |
template<typename T > | |
void | acquire_lock (std::shared_ptr< T > resource) |
Manager to control the lifetime of shared resources.
Resources that need to be available globally, for example via singletons, need to expire after all objects that depend on them have already expired. When static objects reside in different translation units, they can expire in any order, potentially creating race conditions if one static object relies on the other it its destructor.
This class "locks" resources by storing a shared pointer to them, ensuring that the resources lifetime is extended by the lifetime of the class instance. Client code can then keep this class instance alive until the resources are no longer needed, at which point the class instance can be released. Type erasure hides implementation details of the resources being locked.
Multiple resources can be locked, using a LIFO (last-in, first-out) container to ensure that resources are freed in a controlled order. This design choice avoids undefined behavior due to race conditions, which may occur if one global resource's destruction depends on the existence of another global resource. This behavior cannot be achieved with STL containers like std::stack
or std::vector
, since the destruction order of the stored data is under-specified.
Definition at line 52 of file ResourceManager.hpp.
|
default |
|
inline |
Definition at line 74 of file ResourceManager.hpp.
|
inline |
Definition at line 80 of file ResourceManager.hpp.