栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

std::thread使用注意资源回收问题

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

std::thread使用注意资源回收问题

一、线程

1、启动线程存在的固有开销
1. OS分配内核资源、堆栈空间
2. 将线程加入调度器
2、每个线程都需要一份独立的堆栈空间
对于32位系统,一个线程占用1MB的栈空间
3、线程间的任务调度,OS需要做上下文切换
因此,1. 线程启动后,需要关注其销毁
2. 系统资源有些,需要考虑启动线程的数量是否合适

二、thread使用问题

1、启动线程,主线程抛异常,异常退出
例子:

#include 
#include 

using namespace std;

struct func
{
    func(int& i) : i(i) {}
    void operator()() const
    {
        cout << typeid(*this).name() << " " << __func__ << ": " << __LINE__ << " i: " << i << endl;
    }

private:
    int& i;
};

int main(int argc, char const *argv[])
{
    int i = 1;
    func f(i);
    thread t(f);
	// 主线程抛异常退出
    cout << "throw exception start: " << endl;
    throw std::runtime_error("unknown error!!!!!!!!!!");   
    
    return 0;
}

代码中主线程抛异常,提前退出,线程对象t不会调用析构函数销毁线程资源,会导致系统资源泄露。

2、主线程不等待线程退出,先结束执行

int main(int argc, char const *argv[])
{
    int i = 1;
    func f(i);
    thread t(f);
    
    return 0;
}

代码中主线程提前退出,线程对象t调用析构函数提前销毁线程资源,线程执行异常。

三、thread使用防护

需保证主线程退出前等待线程退出(join)或线程已逃逸主线程(detach)
方法一:使用try-catch捕获异常进行防护

int main(int argc, char const *argv[])
{
    int i = 1;
    func f(i);
    thread t(f);
    try
    {
        cout << "throw exception start: " << endl;
       throw std::runtime_error("unknown error!!!!!!!!!!");   
    }
    catch(const std::exception& e)  
    {
        std::cerr << e.what() << 'n';
        t.join();
    }
    
    return 0;
}

方法二:利用RAII机制

#include 
#include 

using namespace std;

struct func
{
    func(int& i) : i(i) {}
    void operator()() const
    {
        cout << typeid(*this).name() << " " << __func__ << ": " << __LINE__ << " i: " << i << endl;
    }

private:
    int i;
};

struct thread_guard
{
    thread_guard(thread& t) : t_(t) {}

    ~thread_guard()
    {
        if (t_.joinable())
        {
            t_.join();
        }
    }

    thread_guard(const thread_guard& ) = delete;
    thread_guard& operator=(const thread_guard& ) = delete;

private:
    thread& t_;
};

int main(int argc, char const *argv[])
{
    int i = 1;
    func f(i);
    thread t(f);
    thread_guard tg(t);

    cout << "start throw exception....." << endl;
    throw std::runtime_error("unknown====================");

    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/298080.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号