Alex Goft
Alex Goft

Reputation: 1124

efficent way for a parent to get its child's id after pthread_create?

Consider the next piece of code -

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

Is there a way for the parent (the main thread) to get its child id? For example right after creating a child in line (*)?

Upvotes: 0

Views: 850

Answers (1)

SergeyA
SergeyA

Reputation: 62583

As per man page, thread id is the same thing as pthread_t returned by pthread_create. So you can just print that:

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

Upvotes: 1

Related Questions