【发布时间】:2021-08-14 10:53:11
【问题描述】:
我想知道如何从创建的线程中获取父 ID。 我的想法是在主函数中创建一个 id 变量,并在创建线程时将其作为参数提供,但它不起作用。 或者我可以通过其他方式获取父 ID?
我的代码:
void first(std::thread::id id) {
//do something
cout << id << endl;
}
int main() {
std::thread::id id = std::this_thread::get_id();
std::thread thread(first, id);
return 0;
}
你有什么想法?
【问题讨论】:
-
为什么这段代码不起作用?
-
你必须在程序终止之前加入你的线程,
thread.join();在return 0;语句之前。
标签: c++ multithreading parent parentid