Raj
Raj

Reputation: 3462

get parent thread Id in case of nested pthread_created

In my C code implementation, I create nested pthreads.

For example:

The application creates threads A, B, and C. Then the threads A creates A0, A1, A2; thread B creates B0, B1 and thread C creates C0, C1, C2, C3.

Now I want to know if it is possible that while executing thread C0, its parent thread Id is that of thread C. or while executing thread A2, its parent thread Id is that of thread A ?

If yes, then what is the possible API for that?

Just on a sidenote, I don't want to use arguments to be passed to the threads for some legacy reasons.

Upvotes: 3

Views: 4685

Answers (1)

alk
alk

Reputation: 70931

There is no such thing as a "parent thread Id".

The PThread specification does not describe a parent<->child relationship between threads. So there are no PThread API calls to pull this information from somewhere.

The pthread-identifier as returned by pthread_self() can be used to destinguish one existing thread from all other existing threads of one process.

Be aware that pthread-identifiers might be recycled during the life-time of a process.

So lets say there are two threads A and B, each having its own different pthread-id ID1 and ID2. If thread A ends and after A ended B creates a new thread C this might very well have ID1 assigned as its pthread-id.

Upvotes: 3

Related Questions