Click to See Complete Forum and Search --> : What happens to threads in a class?
Gyannea
April 29th, 2003, 12:04 PM
What happens if one calls a function in a class that starts a thread which then completes? I assume the thread is gone. Now next time one wants to run the function again, the thread has to be re-created. Will this cause some type of problem or is there something one should do upon thread termination like CloseHandle() or be sure that the thread has (indeed) run to completion?
Andreas Masur
April 29th, 2003, 02:19 PM
Originally posted by Gyannea
What happens if one calls a function in a class that starts a thread which then completes? I assume the thread is gone.
Correct...
Originally posted by Gyannea
Now next time one wants to run the function again, the thread has to be re-created.
Yes...
Originally posted by Gyannea
Will this cause some type of problem or is there something one should do upon thread termination like CloseHandle() or be sure that the thread has (indeed) run to completion?
Well...it depends on several things. Whether you have to close a handle actually depends on the function you used to create the thread. Take a look at this FAQ (http://www.codeguru.com/forum/showthread.php?s=&threadid=231241) for further information.
For the second part...you do not need to wait until the first created thread has terminated. It basically depends on what you are trying to accomplish....but usually you use threads to do things simultanously...however, you need to make sure that (depending on the method used to create the thread) you store each thread handle so that every handle gets closed by a call to 'CloseHandle()'...
Gyannea
April 29th, 2003, 06:37 PM
I use CreateThread() and I do save the handle. My concern is if there are any problems of some leftovers when restarting the thread. Especially in a class instance where the class is not deleted and then the next instance created with "new".
I have a transmitter class which sends an audio signal out the sound card (to go to a radio). A thread is generated to plot the signal as it is sent. When the sending is done the plot thread terminates. I close the thread handle (if it appears to exit) and set the handle to NULL as a check before recreating the thread when I transmit a second message.
I did have some problems about creating the instance of the class in a dialog box and then deleting it upon exit and (I think) the thread not running to completion (never easy to tell when what happens in Windows). I just don't know what Windows does after the thread is done. I recall reading that the thread is NOT entirely gone until one calls CloseHandle().
RockNix
April 30th, 2003, 01:22 AM
the biggest thing which requires seriously design is in case the thread procedure itself when implemented as a member function in a class due to this function has to be in calling convention _stdcall which requires a static function.
once i have written a kind of thread object as abstract base class which encapsulates the thread procedure in a sperate object.
if you want to see about how to do please take a look at my article on:
http://www.klangwerker.de/php/show.php?path=developer/english&doc=e_base_thread1&update=1
Gyannea
April 30th, 2003, 03:51 AM
I have done that with the usual technique of passing a pointer to the instance so I can branch to member functions that are not static but unique to each instance. My concern its just what is left upon termination and what happens on recreation of the thread. I would like each "restart" or re-creation of the thread to be as if it were the first time. Maybe it is and I am concerned about nothing. It's just that haunting statement in the SDK that states "the thread is not completely removed from the operating system until 'CloseHandle(threadhandle)' is called.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.