Re:Improve Multithreads ?
Sasan wrote:
Quote
I have a Thread class that design like this:
FreeOnTerminate ->true
void __fastcall TMyThread::Execute()
{
do
{
}
while ( !JobFinished );
}
OK.
Quote
Now i instance many of this class in my application multithread, i
can change my design to this:
FreeOnTerminate ->false
void __fastcall TMyThread::Execute()
{
// do job
}
void __fastcall TMyThread::OnTerminate()
{
if ( !JobFinished )
this->Resume();
else
delete this;
}
No, it's incorrect: after last '}' in TMyThread::Execute the thread
can't be "restarted", it will be killed in any case. See in Classes.pas:
function ThreadProc(Thread: TThread): Integer;
//...
try
if not Thread.Terminated then
try
Thread.Execute;
except
// ...
end;
finally
/// ...
Thread.DoTerminate; // calls OnTerminate in main thread
// (sinchronized)
// ...
if FreeThread then Thread.Free; // free this TThread instance
// if FreeOnTerminate is true
EndThread(Result); // kill thread in system
end;
end;
And "delete this;" in OnTerminate is error: object will be freed twice:
in OnTerminate and in internal ThreadProc.
Quote
I want know which has better performance for Multithreads instance ?
Your first version:
void __fastcall TMyThread::Execute()
{
do
{
}
while ( !JobFinished );
}
It reuses existent thread (avoids creation of new thread in system).
--
Alex