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
General question on terminating threads
-
- Knows some wx things
- Posts: 30
- Joined: Sun May 04, 2008 6:41 pm
Re: General question on terminating threads
A thread cannot run after an application has exited, however the opposite is possible : the thread running may prevent the application from exitingTexasJimbo 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.
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
"Keyboard not detected. Press F1 to continue"
-- Windows
-- Windows
Re: General question on terminating threads
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
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 itRadek 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.
"Keyboard not detected. Press F1 to continue"
-- Windows
-- Windows