线程和std::shared_ptr智能指针使用

本文详细介绍了如何在C++中使用线程和智能指针进行资源管理。通过示例展示了线程的创建与运行,以及std::unique_ptr和std::shared_ptr的使用方法,帮助读者理解多线程环境下资源的安全管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//定义线程指针
std::unique_ptr<std::thread> thread_;
//指向定义函数
threadCapture_.reset(new std::thread([this]()    
{      
func();         
}
));

#功能函数
void func()
{
    while(1)
    {
        std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
        //compute
      std::chrono::system_clock::time_point expectedNext = now + std::chrono::milliseconds((int)(1000.0f / 30));
			std::this_thread::sleep_until(expectedNext);        
    }
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

 

2.智能指针使用:

class class2: public class1

std::shared_ptr<class1> p1;

p1.reset(new class2);

参考:https://www.cnblogs.com/haippy/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值