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

被折叠的 条评论
为什么被折叠?



