vendredi 13 septembre 2019

Program terminates calling thread join even though thread is joinable

I have the following code:

But for some reason, after running correctly for several iterations, the program keeps terminating during the call to join(), even though joinable() returns true right before the call to join.

I also wrapped the entire block of code for getRank() inside

try{
} catch(const std::exception& e) {
        std::cout << "Caught exception \"" << e.what() << std::endl;
}

But no catch statement ever prints.

This is the output:

========= 1 =========
1
========= 1 =========
1
========= 1 =========
1
========= 1 =========
========= 2 =========
1
<<correct output for iteration 1>>
========= 3 =========
========= 2 =========
1
========= 3 =========
========= 2 =========
1
========= 3 =========
========= 2 =========
1
========= 3 =========
1
========= 1 =========
1
========= 1 =========
1
========= 1 =========
1
========= 1 =========
========= 2 =========
1

^ and the program ends there without any other error messages.

std::thread threadPool[8];


// Create threads and distribute the work across T threads
// -------------------------------------------------------------------
for (int iter = 0; iter < max_iters; iter++)
{        
    for (int i = 0; i < n_workers; i++)
    {
        threadPool[i] = std::thread(getRank, <blah...>);
        std::cout << threadPool[i].joinable() << std::endl;
        std::cout << "========= 1 =========" << std::endl;
    }

    for (int i = 0; i < n_workers; i++)
    {
        std::cout << "========= 2 =========" << std::endl;
        std::cout << threadPool[i].joinable() << std::endl;
        threadPool[i].join();
        std::cout << "========= 3 =========" << std::endl;
    }

}

Aucun commentaire:

Enregistrer un commentaire