this is my first post here after lurking around for the better part of the last two years, so greetings to all the nice people here
Onto my actual question: I'm currently developing a small rendering engine for the company I work at (so unfortunately I can't post any code, sorry guys...) which is coming along rather nicely, but one topic that I'm unhappy with is handling of the resources for renderer (models, textures, shaders etc.). I rather dislike throwing around raw pointers, especially to clients of my renderer, so I initially thought it would be a good idea to use some sort of weak pointers. For this, I'm keeping all pointers to the raw resources in an internal array and give clients objects that act like pointers by holding and index into the internal array. Now, my renderer is working multithreaded and both clients as well as the renderer itself may create or destroy resources at any time, which means I have to lock access to the internal array. This turned out to be a noticable performance hit, since I basically lock a mutex every time someone calls operator->() on any of those pointers.
So I thought about what my resource pointers should be able to handle:
- They should abstract the underlying pointer object and disallow clients to mess with them (no "delete somePtr.Get()" or something like that)
- They have to handle destroyed resources gracefully: The clients are able to free specific resources at any time they want, which should invalidate all pointers to that resource (which is easy with weak pointers, I can simply check if the object at the index exists or not)
- They have to be thread-safe or better yet don't care about threads at all
- Access of the underlying objects has to be fast
I thought about smart pointers, but they're not very well suited for explicit destruction of an underlying object. Also, I don't need reference counting. I also thought about just using an object that encapsulates the raw pointer inside itself, but then I would have no way to find out if the pointer points to something valid or something that has already been destructed.
So if anyone has an idea on how to handle this situation gracefully, I would be happy to know I also appologise if something similar has been asked already, I did only find information on the usual smart pointers online.