How can I get a std::thread
object representing current (already running thread).
I know I can do std::this_thread::get_id()
. However, this will give me a std::thread:id
object.
My main goal is to allow some other thread to join current one. However, the problem is that current one wasn't started by creation of std::thread, so I couldn't have saved it beforehand.
Thread get_id() function in C++ This function returns the value of std::thread::id thus identifying the thread associated with *this. Syntax: thread_name. get_id();
4) The copy constructor is deleted; threads are not copyable. No two std::thread objects may represent the same thread of execution.
A thread does not operate within another thread. They are independent streams of execution within the same process and their coexistence is flat, not hierarchical. Some simple rules to follow when working with multiple threads: Creating threads is expensive, so avoid creating and destroying them rapidly.
std::thread Threads allow multiple functions to execute concurrently. std::thread objects may also be in the state that does not represent any thread (after default construction, move from, detach, or join), and a thread of execution may not be associated with any thread objects (after detach).
You can't get a std::thread
object referring to a thread that wasn't created by the std::thread
constructor. Either consistently use the C++ thread library, or don't use it at all. If the current thread was created by pthread_create
, for example, it will need to be joined to using pthread_join
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With