I want to set up an invocation of a function (or lambda function) to happen automatically when the current thread exits, but I cannot see any way to do it that works with std::thread
unless I take over the entire task of thread creation or manually ensure that every thread calls some particular function that I always provide as its very last operation.
Essentially, I want function whose prototype may resemble something like this:
on_thread_exit(const std::function<void()> &func);
Which would perform whatever setup was necessary to ensure that the given function was automatically called when the thread which called on_thread_exit eventually terminates, and without requiring any particular functions to be explicitly called at thread creation or termination.