title: winapi: get callback when thread ends
body: <p>I have some c++ library code that creates some book-keeping data per thread that accesses it (based on thread-id). I would like to cleanup that data when a thread ends. Is there a way (if not portable, then using win-api) to get notified when a thread ends?</p>
  <p>// simplified example:</p>
  <pre><code>std::mutex mutex_;
  std::unordered_map&lt;std::thread::id, int&gt; thread_accesses_;
  
  void LibFunction() {
    std::thread::id thread_id = std::this_thread::get_id();
    mutex_.lock();
    std::unordered_map&lt;std::thread::id, int&gt;::const_iterator it = thread_accesses_.find(thread_id);
    if(it == thread_accesses_.end()) {
      thread_accesses_[thread_id] = 0;
    } else {
      thread_accesses_[thread_id]++;
    }
    mutex_.unlock();
  }
  </code></pre>