Board index » cppbuilder » Improve Multithreads ?

Improve Multithreads ?


2007-11-19 04:36:06 PM
cppbuilder22
Hello,
I have a Thread class that design like this:
FreeOnTerminate ->true
void __fastcall TMyThread::Execute()
{
do
{
}
while ( !JobFinished );
}
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;
}
I want know which has better performance for Multithreads instance ?
Best Reagrds,
M.T
 
 

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
 

Re:Improve Multithreads ?

Thank you,
If i want terminate thread immediately out of thread class which way is true
or not defer:
1-
MyThread->Suspend();
MyThread->JobFinished = true;
MyThread->Resume();
2-
MyThread->JobFinished = true;
Regards
M.T
"AlexB" < XXXX@XXXXX.COM >wrote in message
Quote
Sasan wrote:

>I have a Thread class that design like this:
>
>FreeOnTerminate ->true
>void __fastcall TMyThread::Execute()
>{
>do
>{
>}
>while ( !JobFinished );
>}

OK.

>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.

>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
 

{smallsort}

Re:Improve Multithreads ?

"Sasan" < XXXX@XXXXX.COM >wrote in message
Quote
if ( !JobFinished )
this->Resume();
You can't restart a thread once it has begun the process of being
terminated. If you are trying to implement thread-pooling, you have to stay
with the approach of having Execute() loop for the lifetime of the thread,
entering a suspended state during the idle times between jobs.
Quote
delete this;
That is not safe to do inside the OnTerminate event. That will produce a
crash.
Gambit
 

Re:Improve Multithreads ?

"AlexB" < XXXX@XXXXX.COM >wrote in message
Quote
And "delete this;" in OnTerminate is error: object will be
freed twice: in OnTerminate and in internal ThreadProc.
Only when FreeOnTerminate is true, which is not the case in that example.
However, it is still dangerous to delete the Thread object inside the event
handler, because the handler is triggered by a call to
TThread::Synchronize(), which needs to continue accessing the Thread object
after the handler has exited.
Gambit
 

Re:Improve Multithreads ?

Sasan wrote:
Quote
If i want terminate thread immediately out of thread class which way
is true or not defer:
1-
MyThread->Suspend();
MyThread->JobFinished = true;
MyThread->Resume();
2-
MyThread->JobFinished = true;
No difference.