父母在pthread_create之后得到孩子的身份的有效方法?

时间:2016-05-04 16:39:46

标签: c multithreading pthreads

考虑下一段代码 -

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;
}

父(主线程)有没有办法获得其子ID?例如,在行(*)?

中创建子项后

1 个答案:

答案 0 :(得分:1)

根据手册页,线程ID与pthread_t返回的pthread_create相同。所以你可以打印出来:

printf("%d", threads[i]);