If main() returns while some threads are still running, what happens? Does the process simply end, killing those threads, or does it continue running until all threads either terminate normally or kill each other?
I'm interested in the behavior of both Linux and BSD, but primarily Linux.
The detached attribute merely determines the behavior of the system when the thread terminates; it does not prevent the thread from being terminated if the process terminates using exit(3) (or equivalently, if the main thread returns). https://man7.org/linux/man-pages/man3/pthread_detach.3.html
What is the main difference between Detach and Join. I did a test, but I cannot see any different behavior for the thread. I am afraid about an eventually memory leak. Is there a significative difference?
join() blocks until the thread function returns. detach() detaches the OS thread from the std::thread object, letting the object be destructed independently of the state of the OS thread's state.
> So using detach() without any object destruction, it generates a persistent memory leak?
No. std::thread::detach()
Separates the thread of execution from the thread object, allowing execution to continue independently. Any allocated resources will be freed once the thread exits https://en.cppreference.com/w/cpp/thread/thread/detach