Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Parent Thread Of Child Thread

Suppose a process creates some threads say threadC and threadD.
Later on each of these threads create new child threads say threadC1, threadC2, threadC3 etc. So a tree of threads will get created.
Is there any way to find out the parent thread of one such randomly created thread?

Actually when our application starts 70 threads gets created which is as per the
requirement.
Later on as per the needs these 70 threads can create more threads.
So apart from these 70 threads a large number of other threads gets created(from
these 70 threads) and destroyed randomly.
So imagine there is a tree of threads.
There is an issue coming up in one such randomly created thread.
On debugging the code we saw that the issue will be due to a value set by its
parent thread.
So we have to find the parent thread of this erroneus thread.

We are not able to get the parent thread id of this thread?

Will anybody let us know how to find the parent of such randomly created threads
using especially windbg/visual studio?
Posted

1 solution

You didn't mention what plaform/language, so I'm assuming C#.

I think the best way to do this would be to pass the creating thread in as a parameter to the ThreadStart method of the child threads. You could make it simple by deriving a new class from the existing .Net Thread class and add a new constructor that accepts a Thread as a parameter.

EDIT ============

To expand, no, there is no way to determine the parent thread of a child thread unless something like what I've mentioned has already been done.

You can put a breakpoint on the line that creates the thread, and manually make a note of the parent thread's ID, along with the new child thread's ID, but that's the best you can expect.

 
Share this answer
 
v3
Comments
rupeshkp728 6-Jan-11 15:02pm    
That could had been a solution.
But its the client code and so we have to debug it as of now.
#realJSOP 6-Jan-11 15:06pm    
All bets are off, then. I think you're simply hosed. There are debugging techniques you can use, but even most of those require some sort of preparation in the source code (setting the Thread.Name property for instance).
rupeshkp728 6-Jan-11 15:18pm    
Platform is Windows.
Lanuage is C++.
Since its the client code and so we have to debug and fix it as of now and not to make any changes in the design of client code.
fjdiewornncalwe 6-Jan-11 15:26pm    
I have a very strong position that a child object should never need to be tightly coupled to the parent by requiring knowledge about them. The parent should always be the one that knows about the children and not vice-versa, but if you had to do that, regardless of language, the logic John presents here is sound.
rupeshkp728 6-Jan-11 15:58pm    
Thanks John and Marcus.
But is not there any way using debuggers to find the parent like we use in process using ppid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900