Thread: Closing Threads

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    7
    Rep Power
    0

    Closing Threads


    Hi All,

    I am new to using threads but got a pretty good handle on them. But i am wondering if you can use the function CloseHandle from the thread function it self. e.g. when you use the CreateThread funciton you have to send a function pointer for it to use. In that funtion i loop until a condition is met to exit the thread. Is it possible to use CloseHandle before you exit the function.

    e.g.

    unsigned long __stdcall threadFunction(void*){

    while(!Terminate){
    ....
    ....
    if(someCondition)
    Terminate = 1;

    }
    }
    // Can i do this here. I saved the handle elsewhere...
    CloseHandle(handletoThread);
    return 1;
    }

    This would make my life so much easier if i can :)

    Regards,
    Siemens

    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    7
    Rep Power
    0
    ooops, just read how one should post...eh, sorry, i will use the php tag next time :)

    Siemens
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    What you are trying to do should work, just remember you can get race conditions and you must declare the variable Terminate as volitile or the compiler may optimize away any attempt to modify it while in use.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    7
    Rep Power
    0

    Threads closing/terminating order


    Ok cool, thanks for the reply. I am just confused about how threads terminate and in which order to do things. The way i understand it is that the thread terminates it self when it exits the thread function?

    However, even if a thread is terminated (by it self) you have to call the CloseHandle function to close it anyway?

    And if so, closeing the handle before the thread function exits is ok?

    Ok a few questions there, but the docs dont seem to give me a clear answer...:)

    Siemens
  8. #5
  9. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I have never used closehandle on a thread, just allowing it to exit when it was done. I am presuming you are in Windows, see if http://msdn.microsoft.com/library/de...losehandle.asp helps any. It seems on reading that closehandle does nothing more than decrement a counter, the thread is still running. Perhaps if you have called gethandle somewhere, the memory won't be released until you call closehandle.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    7
    Rep Power
    0

    Handles


    Hi,

    Ok, will check out the link. I get the handle for the thread when i create it. CreateThread returns it. But if its safte just to let the thread terminate by its self i needent worry :) I was just wondering if not using CloseHandle(HANDLE handle) would led to memory leaks or other types. e.g. since i will have a lot of handles floating around after using CreateThread....

    Siemens
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    15
    Rep Power
    0
    The Handle is good if you want to know when the thread exits by calling WaitForSingleObject().

    If the handle is signaled, you know the thread has exited cleanly. Otherwise something has gone wrong.

    Otherwise just call CloseHandle() after the thread has been created.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    7
    Rep Power
    0
    wow, really? cool, that solves a problem :)

    Cheers,
    Siemens

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo