Page 1 of 1

General question on terminating threads

Posted: Mon Mar 26, 2012 11:50 pm
by TexasJimbo
I want to create a thread to perform general cleanup while my application is running.
In short, my main application creates work files as it processes, and I would like to clean them up when they are no longer needed (I know this sounds like something I could handle in the main code but I have some complexities and this would be a better approach for me and keep my code cleaner).

If I create a detached thread, does it keep running even after the application terminates? Do I have to terminate the thread seperately?
I don't want the thread to keep running when the application ends, so would I have to keep periodically checking to see if the application is running from inside the thread and terminate it when I see the application is no longer running? I haven't worked with threads in a long time and just forget - I think it is part of the application and would terminate when the app terminates, but don't see that in the doc and so am not sure about it.

Thanks in advance.
TexasJimbo

Re: General question on terminating threads

Posted: Tue Mar 27, 2012 1:14 am
by Auria
TexasJimbo wrote: If I create a detached thread, does it keep running even after the application terminates? Do I have to terminate the thread seperately?
I don't want the thread to keep running when the application ends, so would I have to keep periodically checking to see if the application is running from inside the thread and terminate it when I see the application is no longer running? I haven't worked with threads in a long time and just forget - I think it is part of the application and would terminate when the app terminates, but don't see that in the doc and so am not sure about it.
A thread cannot run after an application has exited, however the opposite is possible : the thread running may prevent the application from exiting

the cleanest solution is to make the thread wait on instructions from a thread-safe (priority) queue, then before exiting you can send a 'quit' message and the thread, upon recieving this message, will exit by returning

Re: General question on terminating threads

Posted: Tue Mar 27, 2012 6:39 am
by Radek
You are about to create a "garbage collector". Create a thread, give it the lowest priority possible and let it run in an infinite cycle checking your files and removing the unused ones. During each cycle, the garbage collector will also check TestDestroy() and quit when it obtains true. At the end of each cycle, the garbage collector will call Yield(). This way, you won't burden your CPU unnecessarily. The main thread will call Delete() when the garbage collector isn't needed any more or when the app wants to quit. The garbage collector need not more control.

Re: General question on terminating threads

Posted: Tue Mar 27, 2012 3:56 pm
by Auria
Radek wrote:You are about to create a "garbage collector". Create a thread, give it the lowest priority possible and let it run in an infinite cycle checking your files and removing the unused ones. During each cycle, the garbage collector will also check TestDestroy() and quit when it obtains true. At the end of each cycle, the garbage collector will call Yield(). This way, you won't burden your CPU unnecessarily. The main thread will call Delete() when the garbage collector isn't needed any more or when the app wants to quit. The garbage collector need not more control.
This is generally ok, except that in some cases instead of calling Yield in a loop (busy loop), the thread can just sleep until a new message is sent to it