Reputation: 42379
According to cppref:
std::notify_all_at_thread_exit
provides a mechanism to notify other threads that a given thread has completely finished, including destroying all thread_local objects.
I know the exact semantics of std::notify_all_at_thread_exit
. What makes me puzzled is:
How to register a callback function that will be called after a given thread has finished and destroyed all of its thread-local objects?
Upvotes: 3
Views: 1129
Reputation: 41281
std::notify_all_at_thread_exit
takes a condition variable in its first parameter, by reference. When the thread exits, it will call notify_all
on that condition variable, waking up threads that are waiting for the condition variable to be notified.
There doesn't appear to be a direct way to truly register a callback for this; you'll likely need to have a thread waiting for the condition variable to be notified (using the same lock as the one passed into std::notify_all_at_thread_exit
. When the CV is notified, the thread that's waiting should verify that the wakeup isn't spurious, and then execute the desired code that should be run.
More info about how this is implemented:
At least on Google's libcxx, std::notify_all_at_thread_exit
calls __thread_struct_imp::notify_all_at_thread_exit
, which stores a pair with the parameters to a vector (_Notify
). Upon thread death, the destructor of __thread_struct_imp
iterates over this vector and notifies all of the condition variables that have been registered in this way.
Meanwhile, GNU stdc++ uses a similar approach: A notifier
object is created, it's registered with __at_thread_exit
, it's designed to call its destructor when run at thread exit, and the destructor actually performs the notification process. I'd need to investigate __at_thread_exit
more closely as I don't understand its inner workings fully just yet.
Upvotes: 3