class Test
{
public:
const int &ti;
const int &tw;
Test(int &i, int &w) : ti(i), tw(w) { cout << "调用构造函数" << endl; };
Test(const Test &test) : ti(test.ti), tw(test.tw)
{
cout << "调用拷贝构造函数" << endl;
}
~Test()
{
cout << "调用析构函数" << endl;
}
void operator()()
{
cout << "func 子线程开始" << endl;
cout << "func 子线程处理任务............ " << endl;
for (int i = 0; i < 5; i++) // 处理5s
{
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << "Test::ti " << ti << "Test::tw " << tw << endl;
}
cout << "func 子线程结束" << endl;
}
};
int main(int argc, char **argv)
{
cout << "主线程开始" << endl;
int i = 5;
int w = 2;
Test test(i, w);
thread t(test);
t.detach();
for (int i = 0; i < 3; i++) // 处理3s
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
cout << "主线程3s后结束" << endl;
// 主线程开始
// 调用构造函数
// 调用拷贝构造函数
// func 子线程开始
// func 子线程处理任务............
// Test::ti 5Test::tw 2
// Test::ti 5Test::tw 2
// 主线程3s后结束
// 调用析构函数
// Test::ti 340Test::tw 1170016112// 主线程开始
for (int i = 0; i < 8; i++) // 处理3s
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
cout << "主线程8s后结束" << endl;
主线程开始
调用构造函数
调用拷贝构造函数
func 子线程开始
func 子线程处理任务............
Test::ti 5Test::tw 2
Test::ti 5Test::tw 2
Test::ti 5Test::tw 2
Test::ti 5Test::tw 2
Test::ti 5Test::tw 2
func 子线程结束
调用析构函数
主线程8s后结束
调用析构函数
总结,在主线程,子线程分离的代码中,子线程函数不要 使用 引用和 指针 传递 变量
thread () 对传入的对象进行的拷贝。
void myPrint(const int &li, const char *buf)
// void myPrint(const int &li, const string &buf)
{
cout << "func 子线程开始" << endl;
cout << "func 子线程处理任务............ " << endl;
for (int i = 0; i < 5; i++) // 处理5s
{
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << " myPrint::li " << li << " myPrint::&li " << &li << " myPrint::buf " << buf << " myPrint::&str " << (int *)buf << endl;
}
cout << "func 子线程结束" << endl;
}
int main(int argc, char **argv)
{
cout << "主线程开始" << endl;
int ri = 100;
int &li = ri;
char *buf = "hello world";
cout << " myPrint::li " << li << " main::&li " << &li << endl;
cout << " myPrint::buf " << buf << " main::&buf " << (int *)buf << endl;
thread t(myPrint, li, buf);
t.detach();
for (int i = 0; i < 3; i++) // 处理3s
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
cout << "主线程3s后结束" << endl;
}
// 主线程开始
myPrint::li 100 main::&li 0x228cdffa0c
myPrint::buf hello world main::&buf 0x7ff6315b60cc
func 子线程开始
func 子线程处理任务............
myPrint::li 100 myPrint::&li 0x13497b76170 myPrint::buf hello world myPrint::&str 0x7ff6315b60cc
myPrint::li 100 myPrint::&li 0x13497b76170 myPrint::buf hello world myPrint::&str 0x7ff6315b60cc
主线程3s后结束
myPrint::li 100 myPrint::&li 0x13497b76170 myPrint::buf hello world myPrint::&str 0x7ff6315b60cc
// TAG 演示临时对象的转换发生在子线程中
class A
{
public:
int m_i;
A(int i) : m_i(i)
{
this_thread::sleep_for(std::chrono::seconds(2)); // 让子线程后于主线程结束。 如果转换发生在 主线程,那主线程一定会打印这句话,才会结束掉
cout << "A::A(int i) : 构造函数被执行 " << endl;
}
A(const A &other) : m_i(other.m_i) { cout << "A::A(const A& other) 拷贝构造函数被执行" << endl; }
~A() { cout << "A::~A() 析构函数被执行" << endl; }
};
void func(const int i, const A &a)
{
cout << &a << endl;
return;
}
int main()
{
int i = 25;
int a = 100;
cout << "主线程开始" << endl;
cout << "子线程开始:" << endl;
thread t(func, i, a); // 希望将整型的 a 转换为 A 类型的a 对象
t.detach();
cout << "主程序结束" << endl;
}
// 输出
// 主线程开始
// 子线程开始:
// 主程序结束
// 可见主程序结束前并未打印出A的构造函数,说明 主程序并未发生临时对象的转换。
// TAG 演示临时对象的转换发生在子线程中
class A
{
public:
int m_i;
A(int i) : m_i(i)
{
this_thread::sleep_for(std::chrono::seconds(2)); // 让子线程后于主线程结束。 如果转换发生在 主线程,那主线程一定会打印这句话,才会结束掉
cout << "A::A(int i) : 构造函数被执行 " << endl;
}
A(const A &other) : m_i(other.m_i) { cout << "A::A(const A& other) 拷贝构造函数被执行" << endl; }
~A() { cout << "A::~A() 析构函数被执行" << endl; }
};
void func(const int i, const A &a)
{
cout << &a << endl;
return;
}
int main()
{
int i = 25;
int a = 100;
cout << "主线程开始" << endl;
cout << "子线程开始:" << endl;
thread t(func, i, A(a)); // 希望将整型的 a 转换为 A 类型的a 对象,
// todo 使用 强制类型转换后, 就能使得类型转换在主线程中进行
t.detach();
cout << "主程序结束" << endl;
}
// 输出结果
// 主线程开始
// 子线程开始:
// A::A(int i) : 构造函数被执行
// A::A(const A& other) 拷贝构造函数被执行
// A::~A() 析构函数被执行
// 主程序结束