Ich habe std::sthread
für Hintergrundbild geladen. Ich Hintergrund-Job, da dies zu erstellen:C++ - Threads für Hintergrund laden
if (bgThreads.size() > MAX_THREADS_COUNT){
fclose(file);
return;
}
if (bgThreads.find(id) != bgThreads.end()){
fclose(file);
return;
}
std::shared_ptr<BackgroundPNGLoader> bg = std::make_shared<BackgroundPNGLoader>(file, id);
bgThreads[id] = bg;
bg->thread = std::thread(&BackgroundPNGLoader::Run, bg);
In BackgroundPNGLoader, ich habe:
struct BackgroundPNGLoader{
std::atomic<bool> finished;
FILE * f;
int id;
BackgroundPNGLoader() : finished(false) {}
void Run(){
///.... load data
finished.store(true);
}
}
In meinem Haupt-app, ich habe Update - Render-Schleife in Haupt-Thread ausgeführt wird. In Update habe ich:
std::list<int> finished;
for (auto & it : bgThreads)
{
if (it.second->finished)
{
if (it.second->thread.joinable())
{
it.second->thread.join();
}
finished.push_back(it.first);
//fill image data to texture or whatever need to be done on main thread
fclose(it.second->f);
}
}
for (auto & it : finished)
{
bgThreads.erase(it);
}
Wird dies als sicher angesehen? Ich bin ein wenig besorgt über das Erstellen neuer Threads jedes Mal, wenn ich neue Datei öffnen muss, gibt es keine maximale Grenze.