Thread: How have function called when thread is exiting?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How have function called when thread is exiting?

    Is there a way I can have a function called automatically when the thread it's in exits?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by 6tr6tr View Post
    Is there a way I can have a function called automatically when the thread it's in exits?
    pthread_cleanup_push()

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't the threads themselves "know" when they exit?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    pthread_cleanup_push()
    What about Windows?

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by cpjust View Post
    What about Windows?
    Yeah, is there also a windows functions? What about Mac?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by 6tr6tr View Post
    Yeah, is there also a windows functions? What about Mac?
    You could always research their threading APIs instead of asking others to do it for you.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Don't the threads themselves "know" when they exit?

    --
    Mats
    Not if they're canceled by another thread.

    I immediately assumed POSIX, sorry.

    EDIT: As far as Mac, if it's Mac OS X you've got pthreads, so pthread_cleanup_push() is still the answer.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no function that executes when a thread exits in Windows (at least not using the standard Windows threading API).

    There is the option of using a WaitFor/Single,Multiple/Object() to wait for a thread to finish, and I suppose this will actually work for checking if ANY of the threads have been killed/terminated. It probably requires a separate "watcher" thread to handle the watching, and dealing with the consequences of a particular thread dieing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The best way, I would say is to do it the MFC way.
    Make a CThread class that encapsulates a thread. Then make a virtual function for InitThread and ExitThread that are called when a thread begins and ends respectively. The best way is also to overload start/resume/sleep/kill too, so that if even if you kill the thread, it will call ExtThread first and then kill it (or use a flag).
    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    The best way, I would say is to do it the MFC way.
    Make a CThread class that encapsulates a thread. Then make a virtual function for InitThread and ExitThread that are called when a thread begins and ends respectively. The best way is also to overload start/resume/sleep/kill too, so that if even if you kill the thread, it will call ExtThread first and then kill it (or use a flag).
    But you have to be careful if you use "ExitThread" from a another thread, if you are also use TLS - since TLS uses the "current thread" [1] for it's data.

    [1] Ok, so it's not calling the OS to ask what the current thread is, but rather the OS when it switches from one thread to another, loads FS (I think) with the current threads local storage, so you can offset off that to get your TLS data.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But if CThread knows when a thread exits, then there must be a way to make your own CThread-like class without having to use evil MFC.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    But if CThread knows when a thread exits, then there must be a way to make your own CThread-like class without having to use evil MFC.
    You can't "kill" CWinThread instances, as far as I know. In fact this page seems to confirm this:
    http://msdn2.microsoft.com/en-us/library/2s21xzfe.aspx

    Quote Originally Posted by MSDN
    Terminating a thread prematurely is almost as simple: Call AfxEndThread from within the thread. Pass the desired exit code as the only parameter. This stops execution of the thread, deallocates the thread's stack, detaches all DLLs attached to the thread, and deletes the thread object from memory.

    AfxEndThread must be called from within the thread to be terminated. If you want to terminate a thread from another thread, you must set up a communication method between the two threads.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Is there a way I can have a function called automatically when the thread it's in exits?
    Why?

    gg

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Codeplug View Post
    >> Is there a way I can have a function called automatically when the thread it's in exits?
    Why?

    gg
    Judging from previous threads, presumably to solve some problem with Thread Local Storage variables - but I could be wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    MSDN's own TLS example code uses DLL_THREAD_DETACH. Don't know of anything better for doing something when a thread dies.
    http://msdn2.microsoft.com/en-us/lib...97(VS.85).aspx

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Problem using "cin" in a thread function?
    By Fossil in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2003, 09:08 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM