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

//定义线程指针
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/

发布了95 篇原创文章 · 获赞 25 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/jqw11/article/details/103913330