Qt Multithreaded Learning
Transferred from: http://www.cnblogs.com/IT-BOY/p/3544220.html
Recent projects on the use of multi-threaded knowledge, oneself is also more interested in, so took the "C + + GUI QT4 Programming" to learn.
The 14th chapter of this book is about multithreading, and the QT version used is qt4.x. The next use is the latest QT 5.2, so there are some incompatibilities on the code, a little modification can be run.
QT's multithreading simply means inheriting the Qthread class, overloading the run () function, and starting the thread with start (). First take a look at the first example of the book: (The revised version of the code has been uploaded, click to download)
classThread: Publicqthread{Q_object Public: Thread (QString message="", Qobject *parent =NULL); ~Thread (); voidsetmessage (QString); QString GetMessage (); voidstop ();protected: voidrun ();Private: QString message; volatile BOOLstopped;};
View Code
The thread class inherits the Qthread class and implements the run function. The volatile declaration in front of the stopped variable is stopped as a volatile variable so that each time the stopped is read, it is the most recent value.
Continue to see the implementation of the thread class:
Thread::thread (QString message, Qobject *parent): Stopped (false), Qthread (parent), message (message) {}thread::~Thread () { This-Stop (); This-wait (); Qdebug ()<< This;}voidthread::setmessage (QString message) { This->message =message;} QString Thread::getmessage () {return This-message;}voidthread::stop () {stopped=true;}voidThread::run () { while(!stopped) Std::cerr<<qprintable (message); Stopped=false; Std::cerr<<Std::endl;}
View Code
When initialized, stopped is set to the False,run function to continuously check the value of stopped, which exits when true.
Dialog::D ialog (Qwidget *parent): Qdialog (parent) {Qpushbutton*buttonquit =NewQpushbutton (Qstring::fromlocal8bit ("Quit")); Connect (Buttonquit,&qpushbutton::clicked, This, &dialog::close); Qboxlayout*layout =NewQboxlayout (Qboxlayout::lefttoright, This); Qstringlist List= QString ("ABCDEFGHIJKLMN"). Split ("", Qstring::skipemptyparts); foreach(QString name, list) {Thread*thread =NewThread (Name, This); Qpushbutton*button =NewQpushbutton (QString ("Start") +name, This); Mappingtable.insert (button, thread); Connect (Button,&qpushbutton::clicked, This, &dialog::startorstopthread); Layout-addwidget (button); } Layout-AddWidget (buttonquit); This-setlayout (layout);}voidDialog::startorstopthread () {Qpushbutton*buttonnow = dynamic_cast<qpushbutton*>(Sender ()); Thread*threadnow = (thread*) Mappingtable[buttonnow]; if(Threadnow = = NULL)return; if(threadnow->isrunning ()) {Threadnow-Stop (); Buttonnow->settext (Buttonnow->text (). Replace (QString ("Stop"), QString ("Start")) ); } Else{Threadnow-start (); Buttonnow->settext (Buttonnow->text (). Replace (QString ("Start"), QString ("Stop")) ); }}
View Code
In the Dialog interface class, the button is implemented with the thread one by one corresponding to the connection, in the slot function can easily find the corresponding thread. Where Mappingtable is qmap<qobject*, qobject*> type.
This makes it easy to implement multiple thread modifications, such as:
In addition, the fourth example is also very enlightening to me:
Transactionthread::transactionthread (Qobject *parent): Qthread (parent) {start ();} Transactionthread::~Transactionthread () {{Qmutexlocker locker (&mutex); while(!transactions.isempty ())DeleteTransactions.dequeue (); Transactioncondition.wakeone (); } wait ();}voidTransactionthread::addtransaction (Transaction *transaction) {Qmutexlocker Locker (&mutex); Transactions.enqueue (transaction); Transactioncondition.wakeone ();}voidTransactionthread::run () {Transaction*transaction =0; Qimage oldimage; Forever {{Qmutexlocker locker (&mutex); if(Transactions.isempty ()) transactioncondition.wait (&mutex); if(Transactions.isempty ()) Break; Transaction=Transactions.dequeue (); Oldimage=Currentimage; } Emit transactionstarted (transaction->message (),0); Qimage NewImage= transaction->apply (oldimage); Deletetransaction; {Qmutexlocker Locker (&mutex); Currentimage=NewImage; if(Transactions.isempty ()) emit alltransactionsdone (); } }}voidTransactionthread::setimage (Constqimage&image) {Qmutexlocker Locker (&mutex); Currentimage=image;} Qimage Transactionthread::getimage () {Qmutexlocker locker (&mutex); returncurrentimage;}
View Code
The above is the key code for thread implementation. Mutex mutex variables are used when reading and writing variables shared from the thread and the main threads. The use of Qmutexlocker Locker (&mutex) is also more convenient, in the construction is lock, the destruction of unlock, temporary variables beyond the scope of the natural destruction, have to say that the implementation of the method is very ingenious ah. As for transactioncondition.wait (&mutex) is the waiting condition. Waits for a transaction to join, or to refactor, when the transaction queue is empty. Wake up when you join a transaction, that is, Transactioncondition.wakeone ().
Qt Multithreaded Learning