Qt多線程學習:創建多線程


【為什么要用多線程?】

傳統的圖形用戶界面應用程序都只有一個執行線程,並且一次只執行一個操作。如果用戶從用戶界面中調用一個比較耗時的操作,當該操作正在執行時,用戶界面通常會凍結而不再響應。這個問題可以用事件處理和多線程來解決。

 

【Linux有線程的概念嗎?】

傳統的UNIX系統也支持線程的概念,但一個進程里只允許有一個線程,這樣多線程就是多進程。Linux下的Posix線程(pthreads)是一種輕量級的進程的移植性實現,線程的調度由內核完成,每個線程都有自己的編號。如果使用線程,總體消耗的系統資源較少,線程間通信也比較容易,在工程中推薦使用線程。

 

【使用多線程有什么好處?】

  1. 提高應用程序的響應速度。這對於開發圖形界面程序尤其重要,當一個操作耗時很長時(比如大批量I/O或大量矩陣變換等CPU密集操作),整個系統都會等待這個操作,程序就不能響應鍵盤、鼠標、菜單等操作,而使用多線程技術可將耗時長的操作置於一個新的線程,從而避免上述問題。
  2. 使多CPU系統更加有效。當線程數不大於CPU數目時,操作系統可以調度不同的線程運行於不同的CPU上。
  3. 改善程序結構。一個既長又復雜的進程可以考慮分為多個線程,成為獨立或半獨立的運行部分,這樣有利於程序的理解和維護。

 

【Qt中創建線程的方法】

只需要子類化QThread並重新實現它的run()函數就可以了。run()是個純虛函數,是線程執行的入口,在run()里出現的代碼將會在另外線程中被執行。run()函數是通過start()函數來實現調用的。 

 

【實例】

下面一個例子給出了在應用程序中除了主線程外,還提供了線程A和B。如果單擊窗口中的按鈕“Start A”,Qt的控制台就會連續輸出字母“A”,此時按鈕“Start A”被刷新為“Stop A”。再單擊按鈕“Start B”,控制台會交替輸出字母“A”和“B”。如果再單擊按鈕“Stop A”,則控制台只輸出字母“B”。如下圖所示:

 

 

程序結構

thread.h代碼

 1 #ifndef THREAD_H
 2 #define THREAD_H
 3 
 4 #include <QThread>
 5 #include <iostream>
 6 
 7 class Thread : public QThread
 8 {
 9     Q_OBJECT
10 public:
11     Thread();
12     void setMessage(QString message);
13     void stop();
14 
15 protected:
16     void run(); 
17     void printMessage();
18 
19 private:
20     QString messageStr;
21     volatile bool stopped;
22 };
23 
24 #endif // THREAD_H

注:

  • stopped被聲明為易失性變量(volatile variable,斷電或中斷時數據丟失而不可再恢復的變量類型),這是因為不同的線程都需要訪問它,並且我們也希望確保它能在任何需要的時候都保持最新讀取的數值。如果省略關鍵字volatile,則編譯器就會對這個變量的訪問進行優化,可能導致不正確的結果。

 

thread.cpp代碼

 1 #include "thread.h"
 2 #include <QDebug>
 3 
 4 Thread::Thread()
 5 {
 6     stopped = false;
 7 }
 8 
 9 void Thread::run()
10 {
11     while(!stopped)
12     {
13         printMessage();
14     }
15     stopped = false;
16 }
17 
18 void Thread::stop()
19 {
20     stopped = true;
21 }
22 
23 void Thread::setMessage(QString message)
24 {
25     messageStr = message;
26 }
27 
28 void Thread::printMessage()
29 {
30     qDebug()<<messageStr;
31     sleep(1);
32 }

注:

  • QTread提供了一個terminate()函數,該函數可以再一個線程還在運行的時候就終止它的執行,但不推薦用terminate(),因為terminate()不會立刻終止這個線程,該線程何時終止取決於操作系統的調度策略,也就是說,它可以隨時停止線程執行而不給這個線程自我清空的機會。更安全的方法是用stopped變量和stop()函數,如例子所示。
  • 調用setMessage()讓第一個線程每隔1秒打印字母“A”,而讓第二個線程每隔1秒打印字母“B”。
  • 線程會因為調用printf()而持有一個控制I/O的鎖,多個線程同時調用printf()在某些情況下回造成控制台輸出阻塞,而用qDebug()作為控制台輸出一般不會出現上述問題。

 

threaddialog.h代碼

 1 #ifndef THREADDIALOG_H
 2 #define THREADDIALOG_H
 3 
 4 #include <QPushButton>
 5 #include <QDialog>
 6 #include <QCloseEvent>
 7 #include "thread.h"
 8 
 9 class ThreadDialog : public QDialog
10 {
11     Q_OBJECT
12 
13 public:
14     ThreadDialog(QWidget *parent=0);
15 
16 protected:
17     void closeEvent(QCloseEvent *event);
18 
19 private slots:
20     void startOrStopThreadA();
21     void startOrStopThreadB();
22     void close();
23 
24 private:
25     Thread threadA;
26     Thread threadB;
27     QPushButton *threadAButton;
28     QPushButton *threadBButton;
29     QPushButton *quitButton;
30 };
31 
32 #endif // THREADDIALOG_H

 

threaddialog.cpp代碼

 1 #include "threaddialog.h"
 2 
 3 ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent)
 4 {
 5     threadA.setMessage("A");
 6     threadB.setMessage("B");
 7 
 8     threadAButton = new QPushButton(tr("Start A"), this);
 9     threadAButton->setGeometry(10, 30, 80, 30);
10     threadBButton = new QPushButton(tr("Start B"),this);
11     threadBButton->setGeometry(110, 30, 80, 30);
12     quitButton = new QPushButton(tr("Quit"), this);
13     quitButton->setGeometry(210, 30, 80, 30);
14     quitButton->setDefault(true);
15 
16     connect(threadAButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadA()));
17     connect(threadBButton, SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
18     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
19 }
20 
21 void ThreadDialog::startOrStopThreadA()
22 {
23     if(threadA.isRunning())
24     {
25         threadAButton->setText(tr("Stop A"));
26         threadA.stop();
27         threadAButton->setText(tr("Start A"));
28     }
29     else
30     {
31         threadAButton->setText(tr("Start A"));
32         threadA.start();
33         threadAButton->setText(tr("Stop A"));
34     }
35 }
36 
37 void ThreadDialog::startOrStopThreadB()
38 {
39     if(threadB.isRunning())
40     {
41         threadBButton->setText(tr("Stop B"));
42         threadB.stop();
43         threadBButton->setText(tr("Strat B"));
44     }
45     else
46     {
47         threadBButton->setText(tr("Start B"));
48         threadB.start();
49         threadBButton->setText(tr("Stop B"));
50     }
51 }
52 
53 void ThreadDialog::closeEvent(QCloseEvent *event)
54 {
55     threadA.stop();
56     threadB.stop();
57     threadA.wait();
58     threadB.wait();
59     event->accept();
60 }
61 
62 void ThreadDialog::close()
63 {
64     exit(0);
65 }

注:

  • startOrStopA的邏輯是:當單擊A的按鈕時,如果系統判斷到有線程A在運行中,就把A的按鈕刷新為“Stop A”,表示可以進行stop A的動作,並停止線程A的運行,再將A的按鈕刷新為“Start A”。否則,如果線程A沒有運行,就把按鈕刷新為表示可以運行的“Start A”,啟動線程A,然后將A按鈕刷新為“Stop A”。
  • 當不用Qt設計器時,new一個button出來,需要指定一個父類,比如this,否則運行程序,窗口里沒有按鈕。
  • new了多個按鈕或控件,需要用setGeometry來確定它們的大小和位置,否則前面的被后面的覆蓋,最終看到的是最后一個按鈕。setGeometry的前2個參數是相對於窗口的坐標位置,后兩個參數是按鈕的長寬。
  • 單擊Quit或關閉窗口,就停止所有正在運行的線程,並且在調用函數QCloseEvent::accept()之前等待它們完全結束,這樣就可以確保應用程序是以一種原始清空的狀態退出的。
  • 如果沒有62~65行的重新定義close函數,使進程完全退出。否則點擊Quit按鈕或叉號退出窗口后,進程依然駐留在系統里。

 

main.cpp代碼

 1 #include "threaddialog.h"
 2 #include <QApplication>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication app(argc, argv);
 7     ThreadDialog *threaddialog = new ThreadDialog;
 8     threaddialog->exec();
 9     return app.exec();
10 }

注:

  • 在GUI程序中,主線程也被稱為GUI線程,因為它是唯一一個允許執行GUI相關操作的線程。必須在創建一個QThread之前創建QApplication對象。

 

 

【參考資料】

《C++ GUI Qt 4編程》

《精通Qt4編程》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM