c++11 - Can I make a shared object pool using std::shared_ptr and weak_ptr without a custom destructor? -
I have a pool of shared objects, whose class name is Shadra. I want a "customer" If you are already in the pool, the manager will return a pointer or reference to it, otherwise it creates a new shader and adds it to it before returning its references to the pool . So far it is quite easy.
However, I also want that the shaders should be automatically removed when all the customers have done with them. Is it possible to return the client std :: shared_ptr to the client by implementing the pool as a container of std :: weak_ptr and client? Ie If I call weak_ptr :: lock () several times on the same object, is it share_ptrs that it is connected to each other correctly, or does it return to independent share_ptrs? , But they obviously do not point to its danger, and I think that missed an opportunity to make the weak_PRR very useful.
weak_ptr :: lock () Calling several times OK, returned returned share_ptr is configured correctly , I.e. sharing common pointy reference counter with all shared_patr-s.
Note that lock () gives non-null pointer if there is at least one shared_ptr pointing to the object. If there was no one, then the object would have been destroyed beforehand. Therefore lock () should implement the correct sharing otherwise it will break on every call.
Finally share_ptr supports, they can be useful for removing stale entries from the cache.
Comments
Post a Comment