QThread 创建线程、关闭线程

mac2025-08-28  10

代码如题:

#include <QCoreApplication> #include <QThread> #include <QDebug> class MyThread : public QThread { public: void stop() { //用于结束线程 is_runnable =true; qDebug()<<"thread stop"<<QThread::currentThreadId(); } protected: void run() { while(true) { if(is_runnable) break; qDebug()<<"thread start:"<<QThread::currentThreadId(); } } private: bool is_runnable = false; //是否可以运行 }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qDebug()<<"Main:"<<QThread::currentThreadId(); MyThread m; m.start(); QThread::sleep(1); m.stop(); m.quit(); m.wait(); return 0; }

 

最新回复(0)