lundi 27 novembre 2017

Giving a std::shared_ptr

I have te following code:

void launchThread() {
    std::shared_ptr<std::thread> t;
    t = std::make_shared<std::thread>([t] {std::cout<< "HelloWorld"<<std::endl;});
    t->detach();
}

int main(){
    launchThread();
    somthing that takes a while.... 
}

If I'm correct the thead should keep itself alive using the shared pointer till the thread itself runs out of scope. But I wonder what happens when the shared pointer gets destructed, will the thread be cleaned properly? Or is this bad practice?

Aucun commentaire:

Enregistrer un commentaire