You can't call detach()
on a const std::thread
object, since detach()
needs to modify the state of the std::thread
.
In this case, I would suggest not using a static std::thread
object at all. Especially since it is a one-time-use thread, so the std::thread
object doesn't need to hang around once the thread starts running. Use a local std::thread
variable inside of another function or static method, and then you can call that at startup as needed, eg:
struct AbstractImage
{
private:
static void LoadImages();
struct LoadImagesStarter
{
LoadImagesStarter()
{
std::thread(AbstractImage::LoadImages).detach();
}
};
static const LoadImagesStarter ImageLoader;
};
...
const AbstractImage::LoadImagesStarter AbstractImage::ImageLoader;
void AbstractImage::LoadImages()
{
...
}
Live Demo
Alternatively:
struct AbstractImage
{
private:
static void LoadImages();
friend void LoadImagesStarter();
};
//...
void AbstractImage::LoadImages()
{
...
}
__attribute__((constructor)) void LoadImagesStarter()
{
std::thread(AbstractImage::LoadImages).detach();
}
/* or, if your compiler supports this:
void LoadImagesStarter()
{
std::thread(AbstractImage::LoadImages).detach();
}
#pragma startup LoadImagesStarter 100
*/
Live Demo