I always saw in the internet the rule:
If you don't detach\join a thread, then abort will be called.
I need a reason for why that abort happens.
I can understand with join — because when not doing join to some thread, the the main can be closed before the thread and it can make problems.
But detach doesn't do anything! It has no purpose (at least from what I've seen when running a thread with or without being detached).
What exactly make the abort to jump, any what exactly is the purpose of detach?
Here is a simple example for what causing "aborting":
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
void pause_thread(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "pause of " << n << " seconds ended\n";
}
int main()
{
std::cout << "Spawning and detaching 3 threads...\n";
std::thread (pause_thread,1);
std::cout << "Done spawning threads.\n";
// give the detached threads time to finish (but not guaranteed!):
pause_thread(5);
return 0;
}