我正在从主线程创建一个线程,并将动态分配的对象作为参数。但是如果我们在主线程中删除这个动态内存分配对象,那么我们怎样才能找到在创建的线程中删除的对象。 主线程代码:
int CLocalReader::Run()
{
TReaderArgument *readerArg = new TReaderArgument;
readerArg->iFinished = &theFinishedACE;
readerArg->iSelf = this;
#ifdef WIN32
if (ACE_Thread::spawn((ACE_THR_FUNC)LocalReaderFunc, readerArg) == -1)
{
ACE_DEBUG((LM_DEBUG,"Could not start reader\n"));
delete readerArg;
readerArg = NULL;
}
#else
if (ACE_Thread_Manager::instance()->spawn(ACE_THR_FUNC (LocalReaderFunc), readerArg, THR_NEW_LWP | THR_DETACHED) < 0)
{
ACE_DEBUG((LM_DEBUG,"Could not start reader\n"));
delete readerArg;
readerArg = NULL;
}
#endif
return KErrNone;
}
static void *ReaderFunc(void *arg)
{
ASSERT(arg);
ACE_Thread::yield();
ACE_OS::sleep(ACE_Time_Value(0, STARTUP_TIME));
TReaderArgument *rarg = (TReaderArgument *)arg;
CLocalReader *self = static_cast<CLocalReader *>(rarg->iSelf);
int *finished = rarg->iFinished;
while (!(*finished))
{
if( self->GetData() != KErrorNone )
{
ACE_DEBUG((LM_DEBUG, "%D LocalReader : Error receiving data\n"));
}
}
return 0;
}
如果在上面的代码中,这个对象被删除那么我们如何检查线程函数自身对象被删除
答案 0 :(得分:0)
像在COM中一样使用引用计数。当主线程完成对象时,它可以设置标记“已删除”并释放该对象。该对象尚未删除,因为该线程仍保留引用计数。但是,如果设置了标志,线程可以检查标志并释放对象。引用计数将降至0,并且对象将通过调用delete this;