#include<iostream>
#include<thread>
#include<string>
using namespace std;
class Fctor {
public:
void operator()(string& msg) {
cout << "t1 say: " <<msg<< endl;
msg = "Truse is the mother of deceit. "; //msg的改变并没有改变主线程的输出
}
};
int main() {
string s = "where there is no trust ,there is no love.";
std::thread t1((Fctor()),s);
//std::thread t1((Fctor()), std::ref(s)); //使用std::ref() 达到了子线程和母线程共享变量
t1.join();
cout << "from main: " << s<< endl;
return 0;
}
#include<iostream>
#include<thread>
#include<string>
using namespace std;
class Fctor {
public:
void operator()(string& msg) {
cout << "t1 say: " <<msg<< endl;
msg = "Truse is the mother of deceit. "; //msg的改变并没有改变主线程的输出
}
};
int main() {
string s = "where there is no trust ,there is no love.";
//std::thread t1((Fctor()),s);
std::thread t1((Fctor()), std::ref(s)); //使用std::ref() 达到了子线程和母线程共享变量
t1.join();
cout << "from main: " << s<< endl;
return 0;
}
#include<iostream>
#include<thread>
#include<string>
using namespace std;
class Fctor {
public:
void operator()(string& msg) {
cout << "t1 say: " <<msg<< endl;
msg = "Truse is the mother of deceit. "; //msg的改变并没有改变主线程的输出
}
};
int main() {
string s = "where there is no trust ,there is no love.";
std::thread t1((Fctor()),std::move(s));
//std::thread t1((Fctor()), std::ref(s)); //使用std::ref() 达到了子线程和母线程共享变量
t1.join();
cout << "from main: " << s<< endl;
return 0;
}
#include<iostream>
#include<thread>
#include<string>
using namespace std;
class Fctor {
public:
void operator()(string& msg) {
cout << "t1 say: " <<msg<< endl;
msg = "Truse is the mother of deceit. "; //msg的改变并没有改变主线程的输出
}
};
int main() {
string s = "where there is no trust ,there is no love.";
std::thread t1((Fctor()),std::move(s));
//std::thread t1((Fctor()), std::ref(s)); //使用std::ref() 达到了子线程和母线程共享变量
std::thread t2 = std::move(t1);
t2.join();
cout << "from main: " << s<< endl;
return 0;
}
#include<iostream>
#include<thread>
#include<string>
using namespace std;
class Fctor {
public:
void operator()(string& msg) {
cout << "t1 say: " <<msg<< endl;
msg = "Truse is the mother of deceit. ";
}
};
int main() {
string s = "where there is no trust ,there is no love.";
cout << std::this_thread::get_id() << endl; //获取主线程的ID
std::thread t1((Fctor()),std::move(s));
cout << t1.get_id() << endl;
std::thread t2 = std::move(t1);
cout << t2.get_id() << endl;
t2.join();
cout << "from main: " << s<< endl;
return 0;
}