用于后台加载的C ++线程 [英] C++ threads for background loading

查看:60
本文介绍了用于后台加载的C ++线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用std::sthread进行背景图像加载.我这样创建后台作业:

I have used std::sthread for background image loading. I create background job as this:

 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);    

在BackgroundPNGLoader中,我有:

In BackgroundPNGLoader, I have:

struct BackgroundPNGLoader{
    std::atomic<bool> finished;
    FILE * f;
    int id;
    BackgroundPNGLoader() : finished(false) {}

    void Run(){
       ///.... load data
       finished.store(true);
    }
}

在我的主应用程序中,我有Update-Render循环在主线程中运行.在更新中,我有:

In my main app, I have Update - Render loop running in main thread. In Update, I have:

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);
}

这是否安全?我有点担心每次需要打开新文件时都会生成新线程,没有最大限制.

Is this considered safe? I am a little worried about spawning new threads every time I need new file open, there is no max limit.

推荐答案

首先,最好不要产生比内核更多的线程.

First, you better do not spawn more threads than cores.

这是一个使用分离并让线程通知其状态的简单示例:

Here is a simple example using detach and letting threads informing about their statuses:

#include<thread>
#include<atomic>
struct task_data
{

};
void doHeavyWork(task_data to_do, std::atomic<bool>& done)
{
    //... do work
    //---
    //--
    done = true;
}
int main()
{
    unsigned int available_cores = std::thread::hardware_concurrency();//for real parallelism you should not spawn more threads than cores
    std::vector<std::atomic<bool>> thread_statuses(available_cores);
    for (auto& b : thread_statuses)//initialize with all cores/threads are free to use
        b = true;

    const unsigned int nb_tasks = 100;//how many?
    std::vector<task_data> tasks_to_realize(nb_tasks);
    for (auto t : tasks_to_realize)//loop on tasks to spawn a thread for each
    {
        bool found = false;
        unsigned int status_id = 0;
        while (!found) //loop untill you find a core/thread to use
        {
            for (unsigned int i = 0; i < thread_statuses.size(); i++)
            {
                if (thread_statuses[i])
                {
                    found = true;
                    status_id = i;
                    thread_statuses[i] = false;
                    break;
                }
            }
        }

        //spawn thread for this task
        std::thread task_thread(doHeavyWork, std::move(t), std::ref(thread_statuses[status_id]));
        task_thread.detach();//detach it --> you will get information it is done by it set done to true!
    }

    //wait till all are done
    bool any_thread_running = true;

    while (any_thread_running)//keep untill all are done
    {
        for (unsigned int i = 0; i < thread_statuses.size(); i++)
        {

            if (false == thread_statuses[i])
            {
                any_thread_running = true;
                break;
            }
            any_thread_running = false;
        }
    }
    return 0;
}

这篇关于用于后台加载的C ++线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆