Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I want a function called on thread exit.

I want a function called on thread exit.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I am trying to create my first a multi-threaded dev-studio library project. Is there a way to register a thread "cleanup" function that gets called each time a thread exits? Here are some constraints for any suggestions you provide me: 1) I think I want my library statically linked. I see that for DLLs, DllMain() gets called each time a thread "detaches". This might be an appropriate place for me to stick my clean-up code if I were writing a DLL, but I don't think I want a DLL. 2) I don't want users of my library to have to write any code to get the cleanup function called. It should all be completely hidden. 3) I really only need my cleanup function executed for exiting threads that previously used particular functions from my library. So I do have a place to register the thread-exit function if there is a way to do that at all. Thanks much for your help. Matt Busche

    R 1 Reply Last reply
    0
    • L Lost User

      I am trying to create my first a multi-threaded dev-studio library project. Is there a way to register a thread "cleanup" function that gets called each time a thread exits? Here are some constraints for any suggestions you provide me: 1) I think I want my library statically linked. I see that for DLLs, DllMain() gets called each time a thread "detaches". This might be an appropriate place for me to stick my clean-up code if I were writing a DLL, but I don't think I want a DLL. 2) I don't want users of my library to have to write any code to get the cleanup function called. It should all be completely hidden. 3) I really only need my cleanup function executed for exiting threads that previously used particular functions from my library. So I do have a place to register the thread-exit function if there is a way to do that at all. Thanks much for your help. Matt Busche

      R Offline
      R Offline
      Robert Kuster
      wrote on last edited by
      #2

      > Is there a way to register a thread "cleanup" function > that gets called each time a thread exits? Hm..the first solution that comes to my mind: Call GetExitCodeThread periodically.

      DWORD dwExitCode;
      GetExitCodeThread( hThread, &dwExitCode);

      if( dwExitCode == STILL_ACTIVE )
      // thread still living
      else
      // thread terminated -> do your cleanup

      Anyway, why don't you simply call the cleanup routine at the end of ThreadFunc (just before calling return)?

      DWORD WINAPI ThreadFunc( LPVOID lpParameter )
      {
      // do your work

      MyCleanUpRoutine();
      return 0;
      }

      Regards, RK

      M 1 Reply Last reply
      0
      • R Robert Kuster

        > Is there a way to register a thread "cleanup" function > that gets called each time a thread exits? Hm..the first solution that comes to my mind: Call GetExitCodeThread periodically.

        DWORD dwExitCode;
        GetExitCodeThread( hThread, &dwExitCode);

        if( dwExitCode == STILL_ACTIVE )
        // thread still living
        else
        // thread terminated -> do your cleanup

        Anyway, why don't you simply call the cleanup routine at the end of ThreadFunc (just before calling return)?

        DWORD WINAPI ThreadFunc( LPVOID lpParameter )
        {
        // do your work

        MyCleanUpRoutine();
        return 0;
        }

        Regards, RK

        M Offline
        M Offline
        Matthew Busche
        wrote on last edited by
        #3

        Robert, I guess I'm not a big fan of polling. As for your second suggestion: I'm trying to write a library that other developers will use. My library won't be spawning threads -- the application written by someone else will. So to pursue your suggestion, I'd have to include in my library notes, "oh, and be sure to call MyCleanUpRoutine() before your thread exits. Don't really care for that either. I guess I'm going to have to figure out how to get a DLL to work for me instead. Actually, I think a DLL will work for me, but my initial attempts to create a DLL have not gone so well -- having problems getting global data from the DLL to link. :( Thanks much for your response. Matt Busche

        P 1 Reply Last reply
        0
        • M Matthew Busche

          Robert, I guess I'm not a big fan of polling. As for your second suggestion: I'm trying to write a library that other developers will use. My library won't be spawning threads -- the application written by someone else will. So to pursue your suggestion, I'd have to include in my library notes, "oh, and be sure to call MyCleanUpRoutine() before your thread exits. Don't really care for that either. I guess I'm going to have to figure out how to get a DLL to work for me instead. Actually, I think a DLL will work for me, but my initial attempts to create a DLL have not gone so well -- having problems getting global data from the DLL to link. :( Thanks much for your response. Matt Busche

          P Offline
          P Offline
          Peter Molnar
          wrote on last edited by
          #4

          A *.dll would perfectly suit your needs. What you have to do is: 1.Create a *.dll and export all the functions you want your users to call 2.Make sure that the global data (if any) that simultanious threads can access (i.e. if two or more users at a time call the same function) is protected with syncronization objects like Critical sections 3.If your user calls LoadLibrary, your DllMain gets called with DLL_PROCESS_ATTACH, do some initialization, maybe keep reference count of attached clients, when your user calls FreeLibrary your DllMain gets called with reason DLL_PROCESS_DETACH. This is the place to call your CleanUpFunction() Summary: your user has only to call LoadLibrary on your *.dll, GetProcAddress on the function, and FreeLibrary to get detached from your lib. Peter Molnar

          M 2 Replies Last reply
          0
          • P Peter Molnar

            A *.dll would perfectly suit your needs. What you have to do is: 1.Create a *.dll and export all the functions you want your users to call 2.Make sure that the global data (if any) that simultanious threads can access (i.e. if two or more users at a time call the same function) is protected with syncronization objects like Critical sections 3.If your user calls LoadLibrary, your DllMain gets called with DLL_PROCESS_ATTACH, do some initialization, maybe keep reference count of attached clients, when your user calls FreeLibrary your DllMain gets called with reason DLL_PROCESS_DETACH. This is the place to call your CleanUpFunction() Summary: your user has only to call LoadLibrary on your *.dll, GetProcAddress on the function, and FreeLibrary to get detached from your lib. Peter Molnar

            M Offline
            M Offline
            Matthew Busche
            wrote on last edited by
            #5

            Peter, Thanks for your reply. I actually started to do this and only just now discovered that this only works for RUN-TIME dynamic-linking of a DLL. (I.e., where you use the LoadLibrary call just like you said.) I saw some sample code on how to use LoadLibrary and GetProcAddress to call a single simple function from a DLL, but I'm not exporting a single simple function, I'm exporting a handfull of C++ concrete and abstract classes (along with all their numerous member methods and overloaded operators). It is not clear to me how you'd use run-time linking for such a library. Supppose Peter, you inherited from one of my abstract base-classes, then when you wrote your constructor, how would you call my base class constructor that is exported from my DLL? And even if it's possible, my whole objective is to make my software as easy as possible to use. I certainly wouldn't want my users to have to jump through any flaming hoops to call a base class constructor or call the "=" operator for a class. Here's a thought. What if I had two libraries? Library ALPHA has all my user visible software and could be built arbitrarily for either dynamic or static linkage. If built as a DLL it would be expected to be used with load-time dynamic-linkage. DLL BETA has nothing in it but a DllMain. Now, here's the trick: suppose DLL ALPHA secretly does a LoadLibrary call on BETA via some singleton constructor found in ALPHA. This singleton is instantiated the first time a user of ALHPA references resources that require thread initialization and cleanup activities. Then, I would presume that every subsequent thread spawn and delete would cause this DllMain in BETA to be invoked, would it not? I presume also that DllMain in BETA could invoke methods defined in ALPHA (to effect thread initialization and cleanup.) I'm going to investigate this idea further and maybe try it out. Let me know if I misunderstood the implications of your email. I've been battling this silly problem for a week now. If I don't come up with a better solution soon, I'll have to simply publish a threadCleanup method that must be called by users of my library on thread exit, but I really don't want to have to do this. Once again, thanks sincerely for your help. Matt

            1 Reply Last reply
            0
            • P Peter Molnar

              A *.dll would perfectly suit your needs. What you have to do is: 1.Create a *.dll and export all the functions you want your users to call 2.Make sure that the global data (if any) that simultanious threads can access (i.e. if two or more users at a time call the same function) is protected with syncronization objects like Critical sections 3.If your user calls LoadLibrary, your DllMain gets called with DLL_PROCESS_ATTACH, do some initialization, maybe keep reference count of attached clients, when your user calls FreeLibrary your DllMain gets called with reason DLL_PROCESS_DETACH. This is the place to call your CleanUpFunction() Summary: your user has only to call LoadLibrary on your *.dll, GetProcAddress on the function, and FreeLibrary to get detached from your lib. Peter Molnar

              M Offline
              M Offline
              Matthew Busche
              wrote on last edited by
              #6

              Peter, OK, forget what I just wrote. If just found some words in the MSDN that would seem to imply that DllMain is used even if you use load time dynamic linking of the library. (Which I guess means that LoadLibrary must be invoked for you if you use load-time dynamic linking?) Let me know if I'm lost. Let me know if I'm not lost. I'm forging ahead. Matt

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups