Re[20]: Visual C# vs C++. Надо сравнить перспективы.
От: lpd Черногория  
Дата: 09.01.17 15:32
Оценка: +1 :)
Здравствуйте, alex_public, Вы писали:


_>Эээ что? ) Вот такой простейший пример кода:

_>
_>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
_>


Скомпилировал clang++ в linux и запустил этот код и получил undefined behaviour:

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)

У сложных вещей обычно есть и хорошие, и плохие аспекты.
Берегите Родину, мать вашу. (ДДТ)
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.