一,使用智能指针
class AA{
public:
AA();
~AA();
void StartService();
void EndService();
private:
std::unique_ptr<std::thread> t1_;
std::unique_ptr<NAMESPACE::CalMqtt> Srv_Mqtt_;
};
AA::AA()
{
Srv_Mqtt_.reset(new CalMqtt(CLIENT_ID_SUB));
}
void AA::StartService()
{
LOG_INFO("Start Service.");
t1_.reset(new std::thread([this](){
//std::cout << "t1_" << std::this_thread::get_id() << std::endl;
Srv_Mqtt_->mqtt_running();
}));
}
二,使用实例化,实现相同的功能。
class AA{
public:
AA();
~AA();
void StartService();
void EndService();
private:
std::unique_ptr<std::thread> t1_;
NAMESPACE::CalMqtt aaa;
};
AA::AA()
{
}
void AA::StartService()
{
LOG_INFO("Start Service.");
t1_ = std::make_unique<std::thread>(&NAMESPACE::CalMqtt::mqtt_running,aaa);
}