-
February 3rd, 2010, 01:40 AM
#1
What will happen in long-working thread when main thread exit?
In the main thread when click a button, then begin the long-working thread,
before the long-working thread finish it's work, then the user click the exit button
to exit the application! And in the long-working thread I CreateFile to use all long
with the thread life time.
In such situation, how will the long-working thread know when user exit the application?
how to properly clean up the thread.
Thanks in advance!
-
February 3rd, 2010, 03:37 AM
#2
Re: What will happen in long-working thread when main thread exit?
The usual way is using Events and WaitForSingleObject/WaitForMultipleObjects APIs.
See also Using Worker Threads
Victor Nijegorodov
-
February 3rd, 2010, 03:50 AM
#3
Re: What will happen in long-working thread when main thread exit?
The point is to synchronize the threads. When you close the application you should notify the worker thread to stop. In the GUI thread you should wait for the worker thread(s) to finish and only after that continue closing.
-
February 3rd, 2010, 08:02 AM
#4
Re: What will happen in long-working thread when main thread exit?
To finish up...
If your main thread exits, all other threads that you created will also get aborted right there and then, no cleanup is called. Depending on what your worker thread does, this could lead to all kinds of nasties like unsaved files, corrupted data, resource leaks etc.
In short: the main thread should always wait for worker threads to finish normally. Anything other than that is just sloppy or lazyness on your part at best and can lead to angry customers, lost revenue and long hours wasted on support, debugging and "damage cleanup".
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|