protected: QThread moveDetectorThread; MoveDetector moveDetector; // Некий объект для запуска в потоке
// Запуск цикла объекта при старте потока connect(&moveDetectorThread, &QThread::started, &moveDetector, &MoveDetector::run); // Соединения для корректного завершения потока connect(&moveDetector, SIGNAL(finished()), &moveDetectorThread, SLOT(quit())); connect(&moveDetector, SIGNAL(finished()), &moveDetector, SLOT(deleteLater())); connect(&moveDetectorThread, SIGNAL(finished()), &moveDetectorThread, SLOT(deleteLater())); moveDetector.moveToThread(&moveDetectorThread); // Объект переносится в тред moveDetectorThread.start(); // Тред запускается
void MoveDetector::run(){ exitFlag=false; for(;;){ update(); if(exitFlag) { emit finished(); return; } }}void MoveDetector::doExit(){ exitFlag=true;}
moveDetector.doExit(); while(!moveDetectorThread.isFinished()) { qDebug() << "Wait finished move detector..."; }
Wait finished move detector... [vdso]ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]Wait finished move detector...Wait finished move detector...Wait finished move detector...The program has unexpectedly finished.Процесс был завершён принудительно./home/xi/work/develop/cpp/MoveNoid/build-MoveNoid-Desktop_Qt_5_9_2_GCC_64bit-Debug/MoveNoid crashed.
C++ (Qt) connect(&moveDetector, SIGNAL(finished()), &moveDetector, SLOT(deleteLater())); connect(&moveDetectorThread, SIGNAL(finished()), &moveDetectorThread, SLOT(deleteLater()));
C++ (Qt) moveDetector.doExit();// while(!moveDetectorThread.isFinished()) {// qDebug() << "Wait finished move detector...";// } moveDetectorThread.wait();
C++ (Qt)connect(&moveDetector, SIGNAL(finished()), &moveDetectorThread, SLOT(quit()));
moveDetector.doExit(); moveDetectorThread.wait(); qDebug() << "Success finish.";
C++ (Qt) moveDetector.doExit(); moveDetectorThread.quit(); moveDetectorThread.wait();
C++ (Qt) while(!moveDetectorThread.isFinished()) { qApp->processEvents(); qDebug() << "Wait finished move detector..."; }
C++ (Qt)connect(&moveDetector, &MoveDetector::finished, &moveDetectorThread, &QThread::quit, Qt::DirectConnection);