如何获取当前线程的std :: thread? [英] How to get std::thread of current thread?
问题描述
如何获取表示当前(已运行线程)的std::thread
对象.
How can I get a std::thread
object representing current (already running thread).
我知道我可以做std::this_thread::get_id()
.但是,这会给我一个std::thread:id
对象.
I know I can do std::this_thread::get_id()
. However, this will give me a std::thread:id
object.
我的主要目标是允许其他一些线程加入当前线程.但是,问题在于当前的代码不是通过创建std :: thread开始的,因此我无法事先保存它.
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.
推荐答案
您无法获得std::thread
对象,该对象引用的不是std::thread
构造函数创建的线程.要么始终使用C ++线程库,要么根本不使用它.例如,如果当前线程是由pthread_create
创建的,则需要使用pthread_join
将其加入.
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
.
这篇关于如何获取当前线程的std :: thread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!