DLL中使用线程,自己不能销毁,请高手进来指教

csdnxw 2003-09-28 02:01:23
在DLL中使用线程,出现了一个问题:线程不能自动销毁:

下面是两个测试线程的函数,当在CreateCommThreads和TerminateCommThreads函数执行
ShowMessageBox时,程序正常使用,而且线程能够销毁,但是当把ShowMessage屏蔽之后,
生成的生成的100个线程便不能自动销毁,请各位指教。

/****************************************************************************
测试函数组1:动态开辟数个线程
****************************************************************************/
TCommThread** TestCommThreads = NULL;
static int CreateCommThreadsNum = 100;
//---------------------------------------------------------------------------
int CreateCommThreads(int iThreads = CreateCommThreadsNum)
{
if(NULL != TestCommThreads)
{
return errThreadIsRunning;
}

TestCommThreads = new TCommThread*[iThreads];

for(int i = 0; i < iThreads; i++)
{
TestCommThreads[i] = new TCommThread(false);
TestCommThreads[i]->FreeOnTerminate = true;
//TestCommThreads[i]->Priority = tpNormal;
}

Sleep(100);
//ShowMessage("创建线程完毕");
return errOperationSuccess;
}

//---------------------------------------------------------------------------
int TerminateCommThreads(int iThreads = CreateCommThreadsNum)
{
if(NULL == TestCommThreads)
{
return errThreadIsTerminated;
}

for(int i = 0; i < iThreads; i++)
{
TestCommThreads[i]->ToBeTerminated = true;
//delete TestCommThreads[i];

TestCommThreads[i] = NULL;
}

delete[] TestCommThreads;
TestCommThreads = NULL;

//ShowMessage("over线程完毕");
return errOperationSuccess;
}
/****************************************************************************
测试函数组1:动态开辟数个线程 结束
****************************************************************************/

下面是线程的定义:
.h
//---------------------------------------------------------------------------

#ifndef FCommThreadH
#define FCommThreadH
//---------------------------------------------------------------------------
#include <Classes.hpp>

//----------------------------------------------------------------------
#include "ErrorCode.h"

//---------------------------------------------------------------------------
class TCommThread : public TThread
{
private:
bool mbToBeTerminated;

bool __fastcall FToBeTerminated();
void __fastcall SetToBeTerminated(bool bValue);

protected:
void __fastcall Execute();

public:
__fastcall TCommThread(bool CreateSuspended=false);
__fastcall ~TCommThread();

__property bool ToBeTerminated = {read=FToBeTerminated, write=SetToBeTerminated, nodefault};
void __fastcall CommThreadWorks(void);
};
//---------------------------------------------------------------------------
#endif

/****************************************************************************
备注:

****************************************************************************/
下面是线程的实现:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FCommThread.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TCommThread::TCommThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
FreeOnTerminate = true;
mbToBeTerminated = false;
}

//---------------------------------------------------------------------------
//设置了FreeOnTerminate属性后,会自动执行该函数
__fastcall TCommThread::~TCommThread()
{
Synchronize(CommThreadWorks);
}

//---------------------------------------------------------------------------

/****************************************************************************
__property bool ToBeTerminated = {read=FToBeTerminated, write=SetToBeTerminated, nodefault};
/****************************************************************************/
bool __fastcall TCommThread::FToBeTerminated()
{
return mbToBeTerminated;
}

//---------------------------------------------------------------------------
void __fastcall TCommThread::SetToBeTerminated(bool bValue)
{
this->mbToBeTerminated = bValue;
}

//---------------------------------------------------------------------------
//执行完该函数后,线程自动结束
void __fastcall TCommThread::Execute()
{
while(!this->mbToBeTerminated)
{
Synchronize(CommThreadWorks);
//这里还有很多工作没有作
Sleep(10);
}

return;
}

//---------------------------------------------------------------------------
void __fastcall TCommThread::CommThreadWorks(void)
{
//Application->MessageBoxA("线程执行了析构","提示",MB_OK);
}

/****************************************************************************
请各位高手指教,谢谢。
/****************************************************************************/


...全文
98 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdnxw 2003-10-03
  • 打赏
  • 举报
回复
好,我回去试一下,现在放假不想回公司
csdnxw 2003-10-01
  • 打赏
  • 举报
回复
祝大家节日快乐
shedow 2003-10-01
  • 打赏
  • 举报
回复
应该是程序优化产生临时变量导致 ToBeTerminated出错。可定义ToBeTerminated 为
volatile bool ToBeTerminated ;
gdyt007 2003-10-01
  • 打赏
  • 举报
回复
关注!呵呵
节日快乐
longsoft 2003-10-01
  • 打赏
  • 举报
回复
up
lianghao2 2003-09-30
  • 打赏
  • 举报
回复
关注一下
csdnxw 2003-09-30
  • 打赏
  • 举报
回复
好的
但是好像用Terminate会不跟线程任何反应的机会,不会执行其析够函数啊!
这样的话,我需要清理线程占用的资源,不如开辟的内存空间,打开的文件等
该怎么办?
zihan 2003-09-30
  • 打赏
  • 举报
回复
建议你不要用TestCommThreads[i]->ToBeTerminated 直接用TestCommThreads[i]->Terminate试试,着这用TerminateThread(TestCommThreads[i])试试.
tigerhohoo 2003-09-30
  • 打赏
  • 举报
回复
关注一下.
fatwave 2003-09-29
  • 打赏
  • 举报
回复
学习学习
hy1080 2003-09-28
  • 打赏
  • 举报
回复
关注吧
俺也有同样问题
帮不了你了
帮你顶
csdnxw 2003-09-28
  • 打赏
  • 举报
回复
没有人帮俺么?
大家帮忙啦
虽然现在用Win32 Api而不用BCB封装的TThread来解决这个问题,但是这个问题解决不了,
仍然是个未解决的问题啊!

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧