How to get std :: thread of the current thread?

How can I get an object std::threadrepresenting the current (already running thread).

I know what I can do std::this_thread::get_id(). However, this will give me an object std::thread:id.

My main goal is to allow some other threads to join the current one. However, the problem is that the current one was not started by creating std :: thread, so I could not save it in advance.

+9
source share
3 answers

You cannot get an object std::threadthat refers to a thread that was not created by the constructor std::thread. Either use the C ++ thread library, or don't use it at all. If the current thread was created pthread_create, for example, it will need to be combined with pthread_join.

+9
source

std::threadnot copied. The ability to arbitrarily receive a stream without receiving resources from the initial creation will disrupt the flow.

Any reference to the stream must be or was transferred from the object to which the constructor was called.

+1
source

, std::thread " ", () " "

" " , , main(), std::thread() (, , ). .

" " , std::thread.

" " , , . , "join()" , join() , API (, POSIX) .

In any case, it seems that for good reason you cannot do what you wanted.

+1
source

Source: https://habr.com/ru/post/1669888/


All Articles