(c++ stl thread) what happen when constructing threads without join() or detach()?

code below:

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<thread>

using namespace std;

int main()
{
    thread t1([](){cout<<"t1"<<endl;});
    // without calling t1.join() or t1.detach()
    this_thread::sleep_for(1s);//Mark
    return 0;
}

Q1: what is the (possible) result of this program and why?
Q2: what is the {possible) result of this program erasing the "Mark" line?
I believe the behavior is implementation-defined. On some systems, the process doesn't end until all of its threads terminate.

https://cplusplus.com/forum/unices/285023/
> Q1: what is the (possible) result of this program and why?
> Q2: what is the {possible) result of this program erasing the "Mark" line?

In either case, the destructor of the thread object t1 (t1.joinable() == true) would call std::terminate.
https://en.cppreference.com/w/cpp/thread/thread/~thread

A thread that has finished executing code, but has not yet been joined is still considered an active thread of execution and is therefore joinable.
https://en.cppreference.com/w/cpp/thread/thread/joinable
Please do not post the same question in multiple forums.
https://cplusplus.com/forum/beginner/285230/

Have you tried to run this yourself with and without line 10 to see what happens?
Topic archived. No new replies allowed.