代码拉取完成,页面将自动刷新
/**
* @file thread_3.cpp
* @brief ݲ
*/
#include <iostream>
#include <thread>
using namespace std;
class Widget
{
public:
mutable int v = 0;
Widget() : v(0) { cout << "Widget(), thread id = " << this_thread::get_id() << endl; }
Widget(int i) : v(i) { cout << "Widget(int i), thread id = " << this_thread::get_id() << endl; }
Widget(const Widget& w) : v(w.v) { cout << "Widget(const Widget& w), thread id = " << this_thread::get_id() << endl; }
Widget(Widget&& w) noexcept {
v = w.v;
cout << "Widget(Widget&& w), thread id = " << this_thread::get_id() << endl;
}
void func(const string& s) { cout << "void func(string& s), thread id = " << std::this_thread::get_id() << endl; }
};
void fun(const Widget& w) {
this_thread::sleep_for(chrono::milliseconds(1000));
cout << "thread id = " << this_thread::get_id() << " begin." << endl;
cout << "w.v = " << ++w.v << endl;
cout << "thread id = " << this_thread::get_id() << " endl." << endl;
}
void implicit(const Widget& w)
{
cout << "implicit, thread id =" << std::this_thread::get_id() << endl;
}
int main()
{
cout << "main thread id = " << this_thread::get_id() << " begin." << endl;
Widget w;
cout << "----------------------- thread 1 ----------------------- " << endl;
thread t1(fun, w);
t1.join();
cout << "after thread w.v = " << w.v << endl;
cout << "----------------------- thread 2 ----------------------- " << endl;
thread t2(fun, ref(w));
t2.join();
cout << "after thread w.v = " << w.v << endl;
cout << "----------------------- thread 3 ----------------------- " << endl;
const char* name = "asdf";
thread t3(&Widget::func, &w, name);
t3.detach();
cout << "----------------------- thread 4 ----------------------- " << endl;
thread t4(&Widget::func, &w, string(name));
t4.join();
cout << "----------------------- thread 5 ----------------------- " << endl;
thread t5(implicit, 1);
t5.join();
cout << endl << "main thread id = " << this_thread::get_id() << " end." << endl;
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。