Reputation: 133
I need to create a thread per "TankId" and do some parallel processing in those threads. Once thread is done with its work, I need to destroy the thread and delete the object. I have written following code to create a thread per "TankId". But I am doubtful about how to delete the object of " TLS4_SoapPollClass" class, once thread is done with its job (After finishing Execute function). Do I need to create an array of objets to store the addresses? Please help me to understand the concept.
bool TLS4_SoapManagerClass::CALLBACK_StartThread(int TankId)
{
TLS4_SoapPollClass *_soapPoll = new TLS4_SoapPollClass(TankId);
return true;
}
TLS4_SoapPollClass::TLS4_SoapPollClass(int TankId)
{
int ret = 0;
sprintf(ThreadName,"TankId%d",TankId);
if(InitThread(ThreadName,0))
{
ret = Resume();
}
}
void TLS4_SoapPollClass::Execute()
{
int i = 0;
for(i = 0; i< WMSConfig.PollTankIterations; i++)
{
if (IsItTimeToExit())
{
Debugger.Print(DEBUG_CRITICAL_MSG, "TLS4_SoapPollTank::Execute::Time to
exit. Return tank %hd", this->_tankId);
return;
}
if(!_soap_mgr->CALLBACK_GetReportStatus(this->_tankId))
{
_soap_mgr->AddReqMsg(this->_tankId, TLS4_SOAP_POLL_FOR_TANK_DELIVERIES);
}
else
{
break;
}
ThreadSleep(WMSConfig.PollTankInterval);
}
if(20 == i)
{
_soap_mgr->AddReqMsg(this->_tankId, TLS4_SOAP_REQ_STD_DELIVERIES);
}
_soap_mgr->CALLBACK_SetReportStatus(this->_tankId,0);
Stop();
}
Upvotes: 4
Views: 884
Reputation: 2204
If you want to delete the TLS4_SoapPollClass instance when you're done with that first function, then why create it on the heap? If you create it on the stack a la
TLS4_SoapPollClass _soapPoll(TankId);
then it will be destructed when CALLBACK_StartThread returns (which seems to be the only thing executed in that thread?).
I hope I'm not missing the point of the question, since as others have pointed out, you haven't given us a lot of information.
Upvotes: 0
Reputation: 6849
You can create an array that will hold pointers to objects created in the thread. This array can be created in the main thread and passed to the thread as data pointer (like void*) so that you can typecast it back in the thread & use it.
Once the thread exits, you can clear the memory in the main thread.
Upvotes: 2