CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #10
    Join Date
    Jan 2010
    Posts
    1,133

    Re: while loop before CloseHandle()

    Quote Originally Posted by Uglybb View Post
    Windows does it best to cleanup resources but it is not a guarantee from the MSDN documentation

    [... docs on TerminateThread ...]

    In a very simple or trivial application I agree you could go that route but it is certainly not what I would suggest for commercial products.

    I agree with everything Arjay says and add the small bits of code and thought to sort out a proper close sequence far outways possible problems from bad coding
    Quote Originally Posted by Codeplug View Post
    So normal termination is preferred, but there's little one can do, within the terminating process, to prevent issues that occur due to forceful termination.
    Right - nobody was suggesting forceful termination: a thread will normally terminate when the thread function returns.

    So, the goal is to design the app so that the thread function returns at some point - if this function contains an "infinite" loop, than it's just a matter of modifying the value of a bool variable that controls the loop (when the work is done or not required anymore - set to false).

    As for the handles - these are basically internal system-specific references to kernel objects. Kernel objects are just some data structures used by the OS to keep track of things like threads or processes, and to store some information about them that your application may require. The thread or a process can be long gone, but if some other application still holds the handle, the kernel object will still be there - and this is perfectly normal - an app might process the associated information of a finished thread for one reason or the other. So, a thread kernel object and a thread are associated, but are not one and the same thing.
    Once all the handles are closed, the system will do cleanup, by design. (Since the information is no longer required.)

    As for the possibility of bugs in the OS or a 3rd-party driver: these are very specific cases; again: in normal circumstances, the system will preform cleanup.
    (But, I'm not saying that the app shouldn't do it's own cleanup, too.)

    P.S: BTW: The design advice provided by Arjay is very good.
    Last edited by TheGreatCthulhu; March 23rd, 2011 at 09:26 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured