82 C++线程资源的回收

虽然同一个进程的多个线程共享进程的栈空间,但是,每个子线程在这个栈中拥有自己私有的栈空间。所以,线程结束时需要回收资源。

回收子线程的资源有两种方法:

1)在主程序中,调用join()成员函数等待子线程退出,回收它的资源。如果子线程已退出,join()函数立即返回,否则会阻塞等待,直到子线程退出。

2)在主程序中,调用detach()成员函数分离子线程,子线程退出时,系统将自动回收资源。分离后的子线程不可join()。

用joinable()成员函数可以判断子线程的分离状态,函数返回布尔类型。

子线程的资源一定要回收,如果不回收,会产生僵尸线程,程序还会报错。

#include 
#include 
#include 
using namespace std;
//普通函数void func(int no, const string &str)
{for (int i = 0; i < 10; i++){cout << no << " " << str << endl;sleep(1);}
}int main()
{thread t1(func, 1, "hello");thread t2(func, 2, "world");cout << "start" << endl;for (int i = 0; i < 5; i++){cout << "doing" << endl;sleep(1);}cout << "end" << endl;return 0;  //主线程提前退出,程序运行会报错t1.join(); //回收线程t1的资源t2.join(); //回收线程t2的资源
}
/* start
doing
1 hello
2 world
doing
2 world
1 hello
2 1 hello
world
doing
1 hello
doing
2 world
2 doingworld1 hello
1 endhello2 world
terminate called without an active exception[Done] exited with code=3 in 5.541 seconds */
#include 
#include 
#include 
using namespace std;
//普通函数void func(int no, const string &str)
{for (int i = 0; i < 10; i++){cout << no << " " << str << endl;sleep(1);}
}
int main()
{/*     thread t1(func, 1, "hello");thread t2(func, 2, "world");cout << "start" << endl;for (int i = 0; i < 15; i++){cout << "doing" << endl;sleep(1);}cout << "end" << endl;return 0;  //主线程提前退出,子线程资源没有回收,程序运行会报错t1.join(); //回收线程t1的资源t2.join(); //回收线程t2的资源 *//* thread t1(func, 1, "hello");thread t2(func, 2, "world");cout << "start" << endl;for (int i = 0; i < 5; i++){cout << "doing" << endl;sleep(1);}cout << "end" << endl;t1.join(); //回收线程t1的资源t2.join(); //回收线程t2的资源 */thread t1(func, 1, "hello");thread t2(func, 2, "world");t1.detach();t2.detach();sleep(15);
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部