在Qt中,关闭线程有以下几种方法:
QThread* thread = new QThread;
...
thread->quit();
thread->wait();
QThread* thread = new QThread;
...
thread->requestInterruption();
while (!thread->isInterruptionRequested()) {
// 线程处理逻辑
}
class MyThread : public QThread {
Q_OBJECT
public:
MyThread() : m_stopFlag(false) {}
void stop() {
m_stopFlag = true;
}
protected:
void run() override {
while (!m_stopFlag) {
// 线程处理逻辑
}
}
private:
bool m_stopFlag;
};
MyThread thread;
...
thread.stop();
thread.wait();
以上是一些常见的关闭线程的方法,在实际使用中可以根据具体情况选择合适的方法。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: qt进程间通信的方法是什么