qt 销毁线程处理

基本思路:

当主线程需要销毁子线程时 使用bool变量 来使run函数退出

当run结束后,线程也结束了 触发 finished 我们调用deleteLater来释放申请的内存,

同时将主线程实例化对象的指针释放掉,以便于后续还需要创建该线程

1 主线程的操作

//主线程 实例化一个类pthreat = new threat();QObject::connect(pthreat ,&QThread::finished ,pthreat ,&QObject::deleteLater);pthreat ->Start();

2 子线程类说明 .h文件

class threat: public QThread
{Q_OBJECT
public:threat(QString s,Log *l);~threat();void Start();void Stop();
private:volatile bool Stopthread;protected:void run();
};

子线程类说明 .cpp文件


threat::threat()
{Stopthread = false;
}void threat::run()
{while(!Stopthread){//业务代码}
}
void threat::Start()
{this->start();this->quit();this->wait()}
void threat::Stop()
{Stopthread = true;
}
void zhuxiancheng::destorypthreat()
{if(nul

qt 销毁线程处理

基本思路:

当主线程需要销毁子线程时 使用bool变量 来使run函数退出

当run结束后,线程也结束了 触发 finished 我们调用deleteLater来释放申请的内存,

同时将主线程实例化对象的指针释放掉,以便于后续还需要创建该线程

1 主线程的操作

//主线程 实例化一个类pthreat = new threat();QObject::connect(pthreat ,&QThread::finished ,pthreat ,&QObject::deleteLater);pthreat ->Start();

2 子线程类说明 .h文件

class threat: public QThread
{Q_OBJECT
public:threat(QString s,Log *l);~threat();void Start();void Stop();
private:volatile bool Stopthread;protected:void run();
};

子线程类说明 .cpp文件


threat::threat()
{Stopthread = false;
}void threat::run()
{while(!Stopthread){//业务代码}
}
void threat::Start()
{this->start();this->quit();this->wait()}
void threat::Stop()
{Stopthread = true;
}
void zhuxiancheng::destorypthreat()
{if(nul