The usual way is to set an atomic flag (like std::atomic<bool>
) which is checked by all threads (including the main thread). If set, then the sub-threads exit, and the main thread starts to join
the sub-threads. Then you can exit cleanly.
If you use std::thread
for the threads, that’s a possible reason for the crashes you have. You must join
the thread before the std::thread
object is destructed.