Correct Termination Of A Thread

This is a discussion on Correct Termination Of A Thread within the C++ Programming forums, part of the General Programming Boards category; Good day everyone. I have a general question relating to threads and the necessary steps to correctly "shutdown" a thread. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Post Correct Termination Of A Thread

    Good day everyone. I have a general question relating to threads and the necessary steps to correctly "shutdown" a thread.

    Just quick pseudocode:

    Code:
    HANDLE thread;
    
    thread = CreateThread(task1);
    
    WaitForSingleObject(thread);
    
    closeHandle(thread);    <-------- 
    
    thread = CreateThread(task2);
    
    
    DWORD task1() <--- Thread routine 1.
    DWORD task2() <--- Thread routine 2.
    The line pointed to by the red arrow. Is this required before using the same handle object to create another thread? Or can this call be omitted and just called once upon program termination? Basically wondering about the leaking of GDI resources.

    Thanks.

    Oh I was also wondering. I am quite often shutting down the thread and re-creating it using another processing routine. (Note: Each processing routine does not run in parallel) Should I constantly be shutting down the thread and creating it or just have multiple threads that never shutdown (just suspend) one for each routine or just try and use the one thread to try and handle all routines in there so it lasts the lifetime of the program.? Sorry if this question sounds confusing.
    Last edited by HyperShadow; 07-13-2007 at 12:56 PM. Reason: Additional Question

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Is [the line with the red arrow] required before using the same handle object to create another thread?
    Probably. The assignment would discard whatever thread the previous handle was referencing. The only way this could be freed would be at the end of your program, and even then you might not want to rely on that.

    You could always create test programs -- one of each, CloseHandle() and no CloseHandle() -- and monitor their GDI resources allocated.

    Oh I was also wondering. I am quite often shutting down the thread and re-creating it using another processing routine. (Note: Each processing routine does not run in parallel) Should I constantly be shutting down the thread and creating it or just have multiple threads that never shutdown (just suspend) one for each routine or just try and use the one thread to try and handle all routines in there so it lasts the lifetime of the program.? Sorry if this question sounds confusing.
    Well, since the code does not run in parallel, why would you want to use threads at all?

    Anyway, starting and stopping threads would use less memory than starting them all at once. But starting them all at once might be faster.

    Having only one thread would be ideal, of course. But it might be difficult to implement.
    Last edited by dwks; 07-13-2007 at 01:51 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    BTW, if you plan on using the C standard library at all in your program, consider using _beginthread() or _beginthreadex() instead of CreateThread(). We had a topic about this kind of thing in the C section regarding Windows Threads.

    Edit: Here's the topic: multithreading in c
    Last edited by MacGyver; 07-13-2007 at 02:04 PM.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Thread handles aren't GDI resources. They are kernel resources.

    You must call CloseHandle on every kernel handle created. This includes the return value of CreateThread. Otherwise, you leak the thread descriptor.

    Don't use __beginthread. It's conceptually broken. Always use __beginthreadex.

    Better yet, use Boost.Thread.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Each Win API function allocating some resource like handle explicetly specifies what function should be called to free the resourse... You just need to read the function reference and follow the requirements...

    And BTW - if you do not need the thread handle for any reasn - you can close the handle just after the creating the thread, it will not terminate the thread - just free the resource allocated in the main thread, child thread will free its own resources when it is finished...

    Of course freeing the Handle in such a manner will prevent you from waiting till the thread termination and retreiving the return value of the thread - for example.
    Last edited by vart; 07-13-2007 at 03:04 PM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    41
    Quote Originally Posted by vart View Post
    And BTW - if you do not need the thread handle for any reasn - you can close the handle just after the creating the thread, it will not terminate the thread - just free the resource allocated in the main thread, child thread will free its own resources when it is finished...
    Woah, now that I have never heard of before. I was assuming the handle was directly linked to the thread execution.... Very useful to know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 04:14 AM
  2. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  3. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 04:34 AM
  4. Thread confusion
    By pyrolink in forum C Programming
    Replies: 0
    Last Post: 01-29-2006, 08:42 PM
  5. Thread inside a thread ?
    By lsme in forum Linux Programming
    Replies: 3
    Last Post: 12-08-2004, 10:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21