parent 在 pthread_create 之后获取其 child 的 ID 的有效方法?
efficent way for a parent to get its child's id after pthread_create?
考虑下一段代码 -
pthread_t* threads;
void createWorkers(WorkerType type)
{
// Create mapper threads and their Container
for (int i = 0; i < poolSize; ++i)
{
// Add new Thread
ret = pthread_create(&threads[i], nullptr, function, nullptr);
// HERE THE MAIN THREAD PRINTS THE JUST CREATED THREAD ID (*)
}
}
int main()
{
createWorkers();
// JOINING THE THREADS
return 0;
}
有没有办法让 parent(主线程)获得它的 child id?例如,在 (*)?
行中创建 child 之后
根据手册页,线程 ID 与 pthread_create
返回的 pthread_t
相同。所以你可以打印:
printf("%d", threads[i]);
考虑下一段代码 -
pthread_t* threads;
void createWorkers(WorkerType type)
{
// Create mapper threads and their Container
for (int i = 0; i < poolSize; ++i)
{
// Add new Thread
ret = pthread_create(&threads[i], nullptr, function, nullptr);
// HERE THE MAIN THREAD PRINTS THE JUST CREATED THREAD ID (*)
}
}
int main()
{
createWorkers();
// JOINING THE THREADS
return 0;
}
有没有办法让 parent(主线程)获得它的 child id?例如,在 (*)?
行中创建 child 之后根据手册页,线程 ID 与 pthread_create
返回的 pthread_t
相同。所以你可以打印:
printf("%d", threads[i]);