you can use the api atexit to register when the thread is going to exit, the api is from stdlib.h.
1.here is personal thoughts after some test and debug:
When main-thread executing atexit, other subthreads can keep on running. After main-thread finish atexit, subthreads those are still running will be in dangerous state: they may soon be terminated by system, without finishing their jobs.
2.how to test:
You can create subthreads and sleep for a while, register a function to the api atexit in main-thread. They won't start terminating sleeping until after the function is done.
3.where is the thoughts from:
https://www.openssl.org/docs/man1.1.1/man3/OPENSSL_thread_stop.html#NOTES
On Linux/Unix where OpenSSL has been loaded via dlopen() and the application is multi-threaded and if dlclose() is subsequently called prior to the threads being destroyed then OpenSSL will not be able to deallocate resources associated with those threads. The application should either call OPENSSL_thread_stop() on each thread prior to the dlclose() call, or alternatively the original dlopen() call should use the RTLD_NODELETE flag (where available on the platform).
Under windows, after FreeLibrary(.dll) in main-thread, while calling ~dllModule(){..stopAndWaitThreadPool()} in main-thread, sub-threads can be terminated, leaving resources not recycled in time, resulting in something such as blocking in main-thread in the api stopAndWaitThreadPool()!!! So I guessed that it could be the same under macosx.
However!!! Those subthreads is still running while calling ~dylibModule(){..stopAndWaitThreadPool} and finish jobs before the api stopAndWaitThreadPool! And then I test and debug.
you can read more info about how to call the api OPENSSL_thread_stop
https://blog.csdn.net/newmandirl/article/details/80081210