Board index » cppbuilder » Thread Objects

Thread Objects


2004-05-10 09:58:38 PM
cppbuilder105
After defining a borland thread object, you execute it creating an instance
of it with the new operator. After the thread has finished processing is it
necessary to call delete? From what I understand, by setting
FreeOnTerminate to true inside the thread's constructor, this should take
care of destroying the thread and deallocating any memory and thus making it
unnecessary for me to call delete for the thread instance. Am I
understanding correctly?
Thanks in advance for any assistance you can offer.
-Jorly
 
 

Re:Thread Objects

Hi Jorly,
Quote
After the thread has finished processing is it necessary to call delete?
No. In fact, if you do, your application will certainly hang.
Quote
From what I understand, by setting FreeOnTerminate to true inside the
thread's constructor, this should take care of destroying the thread and
deallocating any memory and thus making it unnecessary for me to call
delete for the thread instance. Am I understanding correctly?
Yes. All you have to do in your main thread, is call the thread's
Terminate() method. After the thread has stopped, it will be automatically
destroyed and memory allocated for the thread object will be freed. If you
allocate any memory inside the thread, make sure you destroy it in the
destructor of the thread object.
Regards,
Martin Nijhoff
 

Re:Thread Objects

Quote

Yes. All you have to do in your main thread, is call the thread's
Terminate() method. After the thread has stopped, it will be automatically
destroyed and memory allocated for the thread object will be freed. If you
allocate any memory inside the thread, make sure you destroy it in the
destructor of the thread object.

When you say my "main thread", do you mean the main function of my
personalized thread, my personalized thread's Execute() method, or the
thread (ie a TForm instance) that spawned my personalized thread?
Thanks,
Jorly
 

{smallsort}

Re:Thread Objects

"Jorly Metzger" < XXXX@XXXXX.COM >wrote in message
Quote
After the thread has finished processing is it necessary to call delete?
That depends on your setting of the FreeOnTerminate property. If set to
true, then no. Otherwise yes.
Quote
From what I understand, by setting FreeOnTerminate to true inside
the thread's constructor, this should take care of destroying the thread
and deallocating any memory and thus making it unnecessary for me to
call delete for the thread instance. Am I understanding correctly?
Correct.
Gambit
 

Re:Thread Objects

"Martin Nijhoff" < XXXX@XXXXX.COM >wrote in message
Quote
No. In fact, if you do, your application will certainly hang.
That is a bit misleading. The FreeOnTerminate property is set to false by
default. If you do not delete the thread yourself, you will be leaking
memory and resource handles. Deleting a thread will NOT hang the
application, unless your thread is using Synchronize() or SendMessage() at
the time of the deletion, in which case you have a potential deadlock. But
you shouldn't be trying to delete the thread while it is still running
anyway, always terminate your threads before freeing them.
Quote
Yes. All you have to do in your main thread, is call
the thread's Terminate() method.
All that does is simply signals the thread that it should terminate itself
at some point in the future. Terminate() does not actually stop the thread
at all.
Quote
After the thread has stopped, it will be automatically destroyed
and memory allocated for the thread object will be freed.
Only if you have the thread's FreeOnTerminate property set to true, which is
not the default value so you have to set it to true yourself manually if you
want that kind of behavior.
Gambit
 

Re:Thread Objects

"Jorly Metzger" < XXXX@XXXXX.COM >wrote in message
Quote
When you say my "main thread", do you mean the main
function of my personalized thread
No.
Quote
my personalized thread's Execute() method
No.
Quote
or the thread (ie a TForm instance) that spawned my
personalized thread?
Yes. Although really, you can free threads for any other thread, it doesn't
have to be the main code. It all depends on your actual application design.
Threads can spawn each other, and free each other, if your design is safe
and solid enough.
Gambit