lundi 7 août 2017

Can a C++11 function return when the threads joined to it still not terminated?

I have a function in my code which is not the main function and uses threads. It is called thousands of times when the program is run. It looks like this

void my_function(vector<string> input, vector<string> &results)
{
   results.clear();



    vector<thread> th;

    th.resize(input.size());


    for(int i=0; i<input.size();i++)
    {
        th[i]=thread(fold, ref(results), i );
    }

    for(auto & t : th)
    {
        t.join();
    }
}    

I want to make sure when my_function returns all the threads are terminated. The reason I ask this question is that after parallelizing this function I get huge memory leaks and am suspecting the threads keep the resources the take.

Aucun commentaire:

Enregistrer un commentaire