C++ 并发编程(1)再学习,为什么子线程不调用join方法或者detach方法,程序会崩溃? 仿函数的线程启动问题?为什么线程参数默认传参方式是值拷贝?
本文的主要学习点,来自 这哥们的视频内容,感谢大神的无私奉献。你可以根据这哥们的视频内容学习,我这里只是将自己不明白的点,整理记录。
C++ 并发编程(1) 线程基础,为什么线程参数默认传参方式是值拷贝?_哔哩哔哩_bilibili
之前的关于 线程 的 理解 有些地方 是有误区的,这里记录并改正一下。
一 不调用join方法或者detach方法,程序会崩溃的原因
来看一个例子
主线程调用test11();也就是test11()方法是在主线程执行。
test11方法中会启动一个子线程 threadtest11,参数为 int。
在test11方法中,不让子线程调用 join 或者 detach 方法,程序会报 error
目的是探寻 程序报 error的点在哪里?
之前的一直认为error 原因是:
主线程 比 子线程早结束。
而这时候子线程还在使用 主线程的资源,但是主线程都结束了,主线程结束,意味着程序就结束,那么子线程还在使用程序中的资源,那么肯定会有问题。
改动代码验证此问题:我们下边的代码 让主线程在启动子线程后,睡5秒钟,
等待子线程运行完成,由于子线程中就打印了两行log,那么一定能保证 子线程 执行完毕后,主线程还在执行。
也就是说,不存在 我们之前认为的 root cause 是 主线程已经结束,但是子线程还在使用 主线程的资源的问题。
int main()
{test11();
}//子线程入口函数
void threadtest11(int threadparams ) {cout << "thread test 11 start threadparams = " << threadparams << endl;cout << "thread test 11 end threadparams = " << threadparams << endl;}//主线程调用
void test11() {cout << "main thread 11 start" << endl;int aa = 80;thread thread1(threadtest11,aa);//if (thread1.joinable()) {// thread1.join();//}this_thread::sleep_for(5000ms);cout << "main thread 11 end" << endl;}
结果是这样的,注意的是,error弹出框是在5秒后弹出的
谁在打印这个 error 信息呢?
再来看一下自己写的这个例子:以及结合我们观察到的 error 框是在5秒之后才弹出来。
那么很有可能是 theard1 的生命周期在 test11方法结束后,需要调用 thread1的析构函数,在这个thread1的析构函数中做的事情。
//主线程调用
void test11() {cout << "main thread 11 start" << endl;int aa = 80;thread thread1(threadtest11,aa);this_thread::sleep_for(5000ms);cout << "main thread 11 end" << endl;
}
观察C++ thread 的析构函数代码:
如果user没有调用 joinable()函数,那么会执行 terminate()
~thread() noexcept {if (joinable()) {_STD terminate();}}
terminate()函数干了啥?
是C++的标准库函数
定义于头文件 | ||
void terminate(); |
terminate()函数在程序抛出一个异常并且异常没有被捕捉的时候被调用.
默认情况下,terminate()函数调用标准C库函数abort()使程序终止而退出。当调用abort函数时,程序不会调用正常的终止函数,也就是说,全局对象和静态对象的析构函数不会执行。
3. C++ terminate()函数-CSDN博客
root cause:只要thread的joinable()方法为 true,就会抛出异常。
怎么改动?-也就是谁能影响 thread的 joinable()的值。
当我们调用了thread1.join();或者thread1.detach()都会影响
到此,我们就将这个error的如何出来的,以及如何fix 都明白了。
问题是:thread.h为什么要在 析构函数中这么设计呢?
我们知道 thread1.join () 是主线程卡在当前问题,等待子线程结束。
thread1.detach()是让线程分离出去。
二 仿函数的线程启动问题
仿函数实际开发中用的不多,这里只是记录。
啥是仿函数?
如果一个类 重写了 operator() 方法,这个类的实例加上(),实际上会执行 重写的 operator()方法,就想函数调用一样,因此叫做仿函数
32 C++ 可调用对象,仿函数 ,functional类型,_仿函数调用-CSDN博客
实际测试 :视频中的问题,在vs2017 上不报错
class Teacher12 {
public://Teacher12 类 重载了 “()”,运算符,可以认为 C++ 中用 operator() 表示“()”,参数是 int valuevoid operator()(int value) {cout << " operator() params = " << value << endl;}
};//主线程调用
void test12() {cout << "main thread 12 start" << endl;Teacher12 t12;int a = 180;thread thread1( t12, a);thread thread2(Teacher12(), a); //视频中的写法,在vs2017 上不报错if (thread1.joinable()) {thread1.join();}if (thread2.joinable()) {thread2.join();}cout << "main thread 12 end" << endl;}
如果c++的编译器版本比较低,遇到了怎么办呢?
C++11会把 {} 包裹的部分认为是 初始化的操作。
//可多加一层()
std::thread t2((Teacher12()));
t2.join();
//可使用{}方式初始化
std::thread t3{ Teacher12() };
t3.join();