Search code examples
androidc++linuxpthreadsthread-local-storage

c++ thread_local destructors with pthread destructors


I want to do some work after all C++ thread_local destructors called. This is platform specific - Android, so I have access to pthreads.

The question is, when pthread_key_created destructors should be called, before or after C++ thread_local destructors? Or they can be interleaved?

I tested On Linux Mint and pthread destructors called after C++ 's.


Solution

  • bionic/pthread_exit.cpp currently has the same order:

    void pthread_exit(void* return_value) {
      // Call dtors for thread_local objects first.
      __cxa_thread_finalize();
      // Call the TLS destructors.
      pthread_key_clean_all();
    

    However, this is not documented behavior and you should not build something relying on it.