Olexy
Olexy

Reputation: 323

Detaching thread from it self

I got some problems with multythreading, while i was creating small Direct2d library. Is there any way to detach thread from it self? For exaple:

std::thread a([](){
std<<cout "thread started" << std::endl;
//here i need to destroy(detach) this thread
});

Upvotes: 0

Views: 190

Answers (1)

Azam Bham
Azam Bham

Reputation: 1399

No, there is no standard or safe way to detach a thread from within the thread. You must either join or detach a thread object before it goes out of scope.

Based on your use case, the use of std::async may be a more suitable alternative, as it does not require any manual joining or detaching of a thread:

#include<future>

std::future<void> fut = std::async(std::launch::async, []{
        std::cout << "asynchronous call" << '\n';
});

//no detaching or joining required

Upvotes: 2

Related Questions