mardi 24 avril 2018

C++ - thread_local vector causes crash on join()

Coming from this question: C++ program crashes sometimes when join()-ing threads

I managed to narrow it down to this very simple program which hopefully you can compile and run easily:

#include <thread>
#include <vector>
using namespace std;

thread_local vector<int> v;

void foo(int n) 
{  
    for(int i=0 ; i<n ; i++)
        v.push_back(i);
}

int main() 
{
    vector<thread> thread_array(4);
    for(int i=0 ; i<4 ; i++)
        thread_array.push_back(thread(foo,100));
    for(int i=0 ; i<4 ; i++)
        thread_array.at(i).join();
    return 0;
}

Why does this program crash after reaching the second for loop (the joining one)? Is this a MinGW bug? As far as I know, I shouldn't be doing anything extra to the thread_local vector. I can post specifics if needed.

Aucun commentaire:

Enregistrer un commentaire