我的儿童线病了吗? [英] Is my Child Thread sick?

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

问题描述

我想做什么?
我只想创建一个子线程,该子线程使用递归遍历方法计算某些文件夹中的子文件夹编号. PATH是我的递归函数的参数

What I want to do?
I just want to creat a child thread,which counts the child folder numbers in some folder with recursive traversal method. The PATH is parameter of my recursive function

void myclass::FolderNum(LPTSTR path)
{
HANDLE hFile;
...
findfirstfile(...);
...
do
{
if(/*a folder*/)
{
...                        //get new PATH;
FolderNum(path);//recrusion
}
}while(findnextfile(...));
closefile(hFile);
}


问题来了!
如果放在主线程中,我的代码运行良好.在子线程中时,它会生病!!!调试时,我发现递归函数PATH的参数没有被推送到线程堆栈中,或者至少在PATH推送时出了点问题.因为当我的代码从第一次递归中跳出时,PATH以乱码出现在了

顺便说一句
VS2008,WTL,除线程回调函数外,我所有的其他函数都是该类的成员函数(这意味着我的线程回调函数是该类的朋友函数.)

所以...
1,有什么建议吗? vs2008调试器问题?我应该使用WinDBG吗?是否有用于调试多线程程序的更好的工具?或者,线程堆栈的尖端,尤其是子线程堆栈...
2,在此先感谢您.
3,对不起,我的英语不好.
添加:
//这些代码在成员函数中,从而创建线程


Here the problem comes!
If put in main thread, my code runs well. While in child thread, it gets sick!!! When debuging, I found the parameter of my recursive function-PATH-were not been pushed into the thread stack,or at least, there''s something wrong when PATH pushed. Because the PATH appears in Garbled characters when my code jump out from first recursion

By the way,
VS2008, WTL, all my functions except thread callback function are member functions of a class(it means, my thread callback function was a friend function of this class.)

So...
1、Any advise? vs2008 debugger problems? Should I use WinDBG? Is there better tools for debugging multithread programs? Or,tips of thread stack especially child thread stack...
2、Thank you in advance.
3、Sorry for my poor English.
ADD:
//these codes are in a member function, creating a thread

TCHAR szCurrentPath[MAX_PATH];       
	GetCurrentDirectory(MAX_PATH,szCurrentPath);	
	dwFolders=1;
	dwFiles=0;
	nPos=0;
	stepLenth=0;


	//Fill the  struct (passed to the thread function)
	threadpara *pa=new threadpara;
	pa->hDll=hDll;
	pa->myself=this;
	pa->szf=szCurrentPath;

	DWORD threadId;
	HANDLE thr;
	thr=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadScan,(LPVOID)pa,0,&threadId);



//ThreadScan是线程函数.



//ThreadScan is the thread function.

void ThreadScan(LPVOID lpv)

{
	Sleep(2000);
	threadpara *p=(threadpara *)lpv;
	//
	myclass* first=p->myself;
	//
	LPTSTR current=p->szf;
	HINSTANCE dll_unload=p->hDll;
	first->FolderNum(current);	
	ExitThread(0);


}

推荐答案

嗯,这是有根据的猜测.没有足够的代码来确定,但是....

在线程内部,您使用当前"作为路径. 当前"是指向字符串的指针".

该指针是从"threadpara"对象的"szf"字段获得的.我假设"szf"也是一个指针"(未显示threadpara的定义).

"szf"是指向"szCurrentPath"数组的指针,该数组似乎是字符串存在的唯一实际位置,也就是说,没有人拥有字符串的* copy *,只有* pointers *的副本字符串.

您不会显示在CreateThread()调用之后会发生什么,但大概是调用线程继续运行.

如果您发布的代码在函数/过程中,则"szCurrentPath"是堆栈上的局部变量.函数返回时,该数组将消失.因此,您很可能会随身携带一个指向已返回到等离子池的字符串的指针,这些字符串已被重用,覆盖,破坏等,因此在线程运行时,它的输入数据就被破坏了. >
在某个时候,有人应该制作路径字符串的更永久副本,或者至少将TCHAR数组定义为寿命比需要它的线程更长的地方,而不是随身携带指针.
Well, here''s an educated guess. There isn''t enough code posted to be really sure but....

Inside your thread, you are using ''current'' as the path. ''current'' is a *pointer* to a string of characters.

That pointer is obtained from the ''szf'' field of your ''threadpara'' object. I''m assuming that ''szf'' is also a *pointer* (the definition of threadpara is not shown).

''szf'' is a pointer to the ''szCurrentPath'' array, which appears to be the only actual place where the string exists, that is, nobody has a *copy* of the string, only copies of *pointers* to the string.

You do not show what happens after the CreateThread() call but presumably the calling thread continues to run.

If the code you posted is inside a function/procedure, then the ''szCurrentPath'' is a local variable on the stack. That array will disappear when the function returns. So, you may very well be carrying around pointers to a string that has been returned to the plasma pool, been reused, overwritten, clobbered, etc so by the time your thread runs, it''s input data is clobbered.

Rather than carry around a pointer, at some point somebody should make a more permanent copy of the path string or at least define the TCHAR array to be somewhere that has a longer lifetime than the thread that needs it.


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

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