(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?
> 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
Topic archived. No new replies allowed.