mardi 26 avril 2016

Terminate a thread from outside in C++11

I am running multiple threads in my C++11 code and the thread body is defined using lambda function as following.

// make connection to each device in a separate child thread
std::vector<std::thread> workers;
for(int ii = 0; ii < numDev; ii++)
{    
    workers.push_back(std::thread([=]() {  // pass by value

     // thread body

    }));
}

// detach from all threads
std::for_each(workers.begin(), workers.end(), [](std::thread &t) {
    t.detach();
});

// killing one of the threads here?

I detached from all children threads but keep a reference of each in workers vector. How can I kill one of the threads later on in my code? I was trying to use std::terminate but I guess it has no use in my case.

Aucun commentaire:

Enregistrer un commentaire