[英]how to get the parent id in c++ with thread?
I want to know how I can get the parent id from a created thread.我想知道如何从创建的线程中获取父 ID。 I had the idea that i make an id variable in the main function and give it as a parameter when i create the thread, but it doesn't work.
我的想法是在主 function 中创建一个 id 变量,并在创建线程时将其作为参数提供,但它不起作用。 Or can i get the parent id otherwise?
或者我可以得到父母的身份证吗?
My code:我的代码:
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;
}
What are your ideas?你有什么想法?
The program该程序
#include <iostream>
#include <thread>
void first(std::thread::id id) {
std::cout << "ID in thread: "<< id << std::endl;
std::thread::id ownID = std::this_thread::get_id();
std::cout << "ID of thread: " << ownID << std::endl;
}
int main() {
std::thread::id id = std::this_thread::get_id();
std::cout << "ID in main: " << id << std::endl;
std::thread thread(first, id);
thread.join();
return 0;
}
generates the output:生成 output:
ID in main: 1
ID in thread: 1
ID of thread: 2
If this isn't the desired output, please clarify your question.如果这不是所需的 output,请澄清您的问题。
By the way: Your idea seems to be the best solution, because even the system does not keep track of parent threads.顺便说一句:您的想法似乎是最好的解决方案,因为即使系统也不跟踪父线程。 Is it possible to get parent threadID from child?
是否可以从孩子那里获取父线程ID?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.