Once the loop creating the threads is done, your program continues. And it continues to leave the main
function which causes all variables defined inside the main
function to go out of scope and their life-time ending. This leads to the destruction of the objects, including the vector which then leads to all the thread object in the vector being destructed.
And as noted by others, the destruction of a thread object will lead to program termination if the thread is not joined or detached.
While the other answers tell you to join the threads (which IMO is the recommended solution here) there is another possible solution: To detach the threads.
While this will lead to the std::terminate
function to be called and prematurely terminate your program, this will lead to another problem, as leaving the main
function ends the process and all its threads.
If you for some reason what your threads to continue to live on after the main
function exits, you need to detach the threads and exit the "main thread" using an operating-system specific function. This will leave the process running with all your created threads still chugging along.