CreateThread() followed by CloseHandle()?

This is a discussion on CreateThread() followed by CloseHandle()? within the C++ Programming forums, part of the General Programming Boards category; This looks like a bug to me, but the MSDN looks a little ambiguous to me. I found some code ...

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    CreateThread() followed by CloseHandle()?

    This looks like a bug to me, but the MSDN looks a little ambiguous to me.
    I found some code that does this:
    Code:
    HANDLE hTcpResponder = CreateThread( NULL, 0, TcpResponder, &ServiceInfo, 0, &TcpResponderId );
    
    if ( hTcpResponder == NULL )
    {
    	emsg( SPNorm, "New Tcp Responder Did not Start" );
    }
    else
    {
    	CloseHandle( hTcpResponder );
    }
    According to the MSDN:
    Quote Originally Posted by MSDN
    The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle.
    Does that mean that you should close the handle AFTER the thread has finished, or can you close it while the thread is still running and then you don't have to worry about when the thread finishes?
    I thought closing the handle would end the thread?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    If closing the handle killed the thread, then I think the program you are examining would not work at all. Not having a thread running isn't exactly a "subtle" bug.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If all handles are closed - thread object will be destroyed as soon as thread is terminated (by reaching return statement of the thread-main function)

    If at least one handle is not closed - thread object is not destructed even after thread is terminated.
    So the application can retrieve the exit code of the thread function using the thread handle...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Thanks, I didn't think something like that could go unnoticed if it was actually a bug; but I'd rather ask and be safe rather than sorry.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,002
    Remember our RAII class friends?
    No messing with closing handles
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Remember our RAII class friends?
    No messing with closing handles
    Yeah, that's usually what I do when I write the code myself. Unfortunately I'm just updating code written over a decade ago by very C-oriented developers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 08:52 AM
  2. What system call use the CreateThread Function of borland?
    By amfm0777 in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2007, 06:41 AM
  3. compiler doesn't like CreateThread
    By finkus in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 03:06 AM
  4. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  5. CreateThread and lpParam
    By Hunter2 in forum Windows Programming
    Replies: 6
    Last Post: 06-02-2004, 05:46 PM

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