0

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

In BackgroundPNGLoader, I have:

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

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

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.

Martin Perry
  • 9,232
  • 8
  • 46
  • 114

2 Answers2

1

First, avoid fopen/fclose and use C++' file I/O instead to avoid potential resource leaks when exceptions are thrown. Further, using a raw std::thread isn't necessary in most cases. For asynchronous tasks, std::async combined with futures is is the thing to go with. std::async returns a potential result within a std::future, which can be retrieved later:

#include <future>
#include <thread>
#include <chrono>
#include <array>
#include <iostream>
#include <random>

size_t doHeavyWork(size_t arg)
{
    std::mt19937 rng;
    rng.seed(std::random_device()());
    std::uniform_int_distribution<unsigned int> rnd(333, 777);
    //simulate heavy work...
    std::this_thread::sleep_for(std::chrono::milliseconds(rnd(rng)));
    return arg * 33;
}

//wrapper function for checking whether a task has finished and
//the result can be retrieved by a std::future
template<typename R>
bool isReady(const std::future<R> &f)
{
    return f.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
}

int main()
{
    constexpr size_t numTasks = 5;

    std::array<std::future<size_t>, numTasks> futures;

    size_t index = 1;
    for(auto &f : futures)
    {
        f = std::async(std::launch::async, doHeavyWork, index);
        index++;
    }

    std::array<bool, numTasks> finishedTasks;
    for(auto &i : finishedTasks)
        i = false;

    size_t numFinishedTasks = 0;

    do
    {
        for(size_t i = 0; i < numTasks; ++i)
        {
            if(!finishedTasks[i] && isReady(futures[i]))
            {
                finishedTasks[i] = true;
                numFinishedTasks++;
                std::cout << "task " << i << " ended with result " << futures[i].get() << '\n';
            }
        }
    }
    while(numFinishedTasks < numTasks);

    std::cin.get();
}

std::async may launch separate threads using a thread pool.

The Techel
  • 918
  • 8
  • 13
1

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