title: pthread_key_create destructor not getting called
body: <p>As per <a href="http://www.manpagez.com/man/3/pthread_key_create/" rel="noreferrer" title="pthread_key_create">pthread_key_create</a> man page we can associate a destructor to be called at thread shut down. My problem is that the destructor function I have registered is not being called. Gist of my code is as follows.</p>
  
  <pre><code>static pthread_key_t key;
  static pthread_once_t tls_init_flag = PTHREAD_ONCE_INIT;
  
  void destructor(void *t) {
    // thread local data structure clean up code here, which is not getting called
  }
  
  void create_key() {
    pthread_key_create(&amp;key, destructor);
  }
  
  // This will be called from every thread
  void set_thread_specific() {
  
    ts = new ts_stack; // Thread local data structure
  
    pthread_once(&amp;tls_init_flag, create_key);
    pthread_setspecific(key, ts);
  }
  </code></pre>
  
  <p>Any idea what might prevent this destructor being called? I am also using atexit() at moment to do some cleanup in the main thread. Is there any chance that is interfering with destructor function being called? I tried removing that as well. Still didn't work though. Also I am not clear if I should handle the main thread as a separate case with atexit. (It's a must to use atexit by the way, since I need to do some application specific cleanup at application exit)</p>