子线程的父线程 [英] Parent Thread Of Child Thread

查看:104
本文介绍了子线程的父线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

子线程的父线程

假设一个进程创建了一些线程,例如threadC和threadD.
稍后在这些线程中的每个线程上创建新的子线程,例如threadC1,threadC2,threadC3等.因此将创建线程树.
有什么方法可以找出一个这样随机创建的线程的父线程吗?

实际上,当我们的应用程序启动时,会按照
创建70个线程 要求.
以后根据需要,这70个线程可以创建更多线程.
因此,除了这70个线程外,还会创建大量其他线程(来自
这70条线)并随机销毁.
想象一下,有一棵线程树.
在这样一个随机创建的线程中出现一个问题.
在调试代码时,我们发现问题是由于其
设置的值引起的 父线程.
因此,我们必须找到该错误线程的父线程.

我们无法获得该线程的父线程ID?

有人会让我们知道如何找到这种随机创建的线程的父级吗?

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?

推荐答案

您没有提到平台/语言,所以我假设使用C#.

我认为最好的方法是将创建线程作为参数传递给子线程的ThreadStart方法.您可以通过从现有的.Net Thread类派生一个新类并添加一个接受Thread作为参数的新构造函数来使其变得简单.
编辑============

为了扩展,没有,除非已经完成了我提到的操作,否则无法确定子线程的父线程.

您可以在创建线程的行上放置一个断点,并手动记下父线程的ID以及新的子线程的ID,但这是您所期望的.

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.


这篇关于子线程的父线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆