class TextThread : public QThread { public: TextThread( const QString &text ); void run(); private: QString m_text; };
bool stopThreads =false;
TextThread::TextThread(constQString&text ):QThread(){m_text = text;}
voidTextThread::run(){ while(!stopThreads ){ qDebug()<< m_text; sleep(1);} }
int main( int argc, char **argv ) { QApplication app( argc, argv ); TextThread mohammed( "mohammed" ), alabdaly( "alabdaly" ); mohammed.start(); alabdaly.start(); QMessageBox::information( 0, "Threading", "Close me to stop!" ); stopThreads = true; foo.wait(); bar.wait(); return 0; }
void TextThread::run() { while ( !stopThreads ) { mutex.lock(); if ( stopThreads ) { mutex.unlock(); return; } qDebug() << m_text; sleep( 1 ); mutex.unlock(); } }