General question on terminating threads

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
TexasJimbo
Knows some wx things
Knows some wx things
Posts: 30
Joined: Sun May 04, 2008 6:41 pm

General question on terminating threads

Post 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
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: General question on terminating threads

Post 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
"Keyboard not detected. Press F1 to continue"
-- Windows
Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: General question on terminating threads

Post 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.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Re: General question on terminating threads

Post 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
"Keyboard not detected. Press F1 to continue"
-- Windows
Post Reply