I have a texture loading thread, which receives requests to load textures from a main thread via a concurrent queue.
The texture loader request is a simple struct with a raw pointer to the object that will receive the texture:
struct TextureLoaderRequest
{
std::string mFilename;
ContentViewer *mContentViewer;
};
The actual texture object contained within the ContentViewer is protected by a mutex and some atomic booleans (also contained within ContentViewer):
std::atomic<bool> mIsLoaded;
std::atomic<bool> mIsVisible;
std::mutex mImageMutex;
Then the texture access routines are as follows:
void ContentViewer::setTexture(ci::gl::TextureRef texture)
{
std::lock_guard<std::mutex> guard(mImageMutex);
mImage = texture;
}
ci::gl::TextureRef ContentViewer::getTexture()
{
std::lock_guard<std::mutex> guard(mImageMutex);
if (mIsVisible)
{
if (mImage != nullptr)
{
mIsLoaded = true;
return mImage;
}
mIsLoaded = false;
}
return nullptr;
}
The texture loader may receive many texture load requests from the main thread at a single time, and then works through the queue loading and assigning textures to the content viewer pointed to in the texture load request message.
The problem I am having is that when the main thread "deletes" the content viewers, the texture loading thread may have an outstanding request in its queue, and by the time it gets to processing it, the content viewer has been deleted and the program crashes.
I'm not sure how to go about removing that outstanding texture load request sitting in the texture thread work queue. I cannot allow the main thread to wait for the relevant texture to be loaded for the content viewer, but then, what would the best practice strategy for achieving this be?
Thanks - Laythe