|
|
От: |
lpd
|
|
| Дата: | 09.01.17 15:32 | ||
| Оценка: |
+1
|
||
_>struct Test{
_> string data;
_> Test(const string& s): data(s) {cout<<this_thread::get_id()<<" constructor: "<<data<<endl;}
_> Test(const Test& t) {data=t.data; cout<<this_thread::get_id()<<" copy constructor: "<<data<<endl;}
_> Test(Test&& t) {data=move(t.data); cout<<this_thread::get_id()<<" move constructor: "<<data<<endl;}
_> ~Test() {cout<<this_thread::get_id()<<" destructor: "<<data<<endl;}
_> void print() {cout<<this_thread::get_id()<<" print: "<<data<<endl;}
_>};
_>int main()
_>{
_> thread second, third;
_> second=thread([&]{
_> Test test("hello world");
_> test.print();
_> third=thread([&](Test&& t){
_> second.join();//ждём завершения родительского потока, чтобы всё было по честному (было ясно, что данные берутся не из его стека)
_> t.print();
_> }, move(test));
_> });
_> second.join();
_> third.join();
_>}
_>_>2 constructor: hello world
_>2 print: hello world
_>2 move constructor: hello world
_>2 move constructor: hello world
_>2 destructor:
_>2 destructor:
_>3 print: hello world
_>3 destructor: hello world
_>140353170302720 constructor: hello world
140353170302720 print: hello world
140353170302720 move constructor: hello world
140353170302720 move constructor: hello world
140353170302720 destructor:
140353170302720 destructor:
terminate called after throwing an instance of 'std::system_error'
what(): Invalid argument
Aborted (core dumped)