[Solved] Is it possible to get parent threadID from child? [closed]


Terminology correction: one thread may create another thread, not fork, which is usually
used to mention one process forking another.

No, a thread has no way to get another thread’s identifier. On Linux, you can check if gettid() == getpid() to find if it’s the main thread. Solaris has thr_main() to identify if the caller is main thread or not. FreeBSD has pthread_main_np() for the same purpose.

But there’s no way to identify parent-child relationship between any threads. Any thread can create more threads. You’ll have to use pass the thread identifiers around when creating threads or use global data structure to maintain this information.

1

solved Is it possible to get parent threadID from child? [closed]