Qt5教程: (9) Qt多线程

0. 建立工程

先建立一个工程吧, 具体步骤前面讲过不少次了, 就再也不细说了。框架

而后在Header文件夹下建立添加一个头文件, 右键Headers -> Add New... -> C++ -> C++ Header File -> Choose函数

随便起个名字, 好比mythread, 而后点Next->Finish。ui

1. QThread 源码一览

mythread.h中包含QThread头文件:操作系统

按住Ctrl键, 点击QThread, 再按住Ctrl键点击qthread.h进入到qthread.h文件, 源码就在这里了, 随便看看就好。哪里不懂就鼠标点一下不懂的地方, 而后按F1, 会跳转到相应的帮助文档,里面讲得很详细, 里面的英文也比较简单。线程

2. QThread相关方法介绍

2.1 启动线程

  • void start(Priority = InheritPriority);
    • 经过调用start()方法来启动线程,该方法会调用run()函数(能够看到QThread中run()为虚函数, 须要咱们来重载)。code

    • run()函数可调用exec()让该线程进入事件循环。orm

    • Priority为线程优先级(下面会讲)。对象

2.2 关闭线程

  • void exit(int retcode = 0);
    • 使线程退出事件循环, 若是该线程没有事件循环, 不作任何操做。
    • retcode默认为0, 表示正常返回。而非0值表示异常退出。
  • void quit();
    • 至关于exit(0)
  • void terminate();
    • 由操做系统强行终止该线程, 可能会致使没法完成一些清理工做, 不推荐使用。
  • void requestInterruption(); + bool isInterruptionRequested();
    • Qt5的新接口, requestInterruption用于请求线程进行中断。isInterruptionRequested返回true/false, 用于判断是否有终止线程的请求。

2.3 阻塞线程

  • bool wait(unsigned long time = ULONG_MAX);
    • 阻塞线程time毫秒, 默认永久阻塞;
    • 只有当线程结束(从run函数返回), 或阻塞超时才会返回;
    • 线程结束或还未启动, wait返回值为true, 超时的返回值为false。
  • static void sleep(unsigned long);
    • 阻塞xx秒, 无返回值。
  • static void msleep(unsigned long);
    • 阻塞xx毫秒, 无返回值。
  • static void usleep(unsigned long);
    • 阻塞xx微秒, 无返回值。

2.4线程状态判断

  • bool isFinished() const;
    • 若是线程结束返回true, 不然返回false。
  • bool isRunning() const;
    • 若是线程正在运行返回true, 不然返回false。
  • bool isInterruptionRequested() const;
    • 若是有终止线程的请求返回true, 不然返回false; 请求可由requestInterruption()发出。

2.5 设置优先级

  • void setPriority(Priority priority);blog

    • 用于设置正在运行的线程的优先级, 若是线程未运行, 则该返回不会执行任何操做并马上返回。可用start(priority)启动带优先级的线程。

    • 指定的优先级是否生效取决于操做系统的调度, 若是是不支持线程优先级的系统上, 优先级的设置将被忽略。

    • 优先级能够设置为QThread::Priority内除InheritPriortyd的任何值:

      QThread::Priority枚举元素 描述
      QThread::IdlePriority 0 没有其它线程运行时才调度
      QThread::LowestPriority 1 比LowPriority调度频率低
      QThread::LowPriority 2 比NormalPriority调度频率低
      QThread::NormalPriority 3 操做系统的默认优先级
      QThread::HighPriority 4 比NormalPriority调度频繁
      QThread::HighestPriority 5 比HighPriority调度频繁
      QThread::TimeCriticalPriority 6 尽量频繁的调度
      QThread::InheritPriority 7 使用和建立线程一样的优先级(这是默认值)

2.6 信号

  • void started(QPrivateSignal);
    • 在线程start后, 执行run前发出该信号。
  • void finished(QPrivateSignal);
    • 在线程结束, 彻底退出前发送此信号。

3. 建立线程

3.1 继承QThread方式

a. 定义MyThread类

mythread.h中定义MyThread类, 并继承QThread, 而后把框架写好:

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>

class MyThread : public QThread
{
    Q_OBJECT

public:
    MyThread();

private:

protected:
    void run();

signals:

public slots:


};

#endif // MYTHREAD_H

b. 重载run()

新建一个C++ Source File, 命名为mythread.cpp

mythread.cpp代码以下, run()函数中咱们让它每隔1秒打印一次字符串:

#include "mythread.h"

// 构造函数
MyThread::MyThread()
{

}

void MyThread::run()
{
    while (!isInterruptionRequested())
    {
        qDebug() << "Running...";
        sleep(1);
    }

    qDebug() << "Get Interruption Request, I'll exit.";
}

由于用到了qDebug(), 别忘了在mythread.h中添加<QDebug>头文件:

#include <QDebug>

c. 开始和结束线程

mainwindow.h中添加头文件和声明变量:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "mythread.h"  // 添加头文件

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    MyThread *my_thread;  // 声明变量
};

#endif // MAINWINDOW_H

mainwindow.cpp中开启和结束线程:

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    my_thread = new MyThread;  // 实例化
    my_thread->start();  // 开启线程


    // 主线程阻塞5秒
    QDateTime start = QDateTime::currentDateTime();
    QDateTime now;
    do {
        now = QDateTime::currentDateTime();
    } while(start.secsTo(now) < 5);


    // 关闭线程
    my_thread->requestInterruption();
    my_thread->wait();

}

MainWindow::~MainWindow()
{

}

由于用到了<QDateTime>, 别忘了在mainwindow.h中添加头文件:

#include <QDateTime>

运行结果:

能够看到主线程被阻塞了5秒, 以后才弹出窗口。可是在主线程阻塞期间, 咱们的my_thread线程仍在运行, 直到线程被关闭。

附: Qt4适用写法

上面咱们结束线程使用的是requestInterruption()isInterruptionRequested(), 这是Qt5新增的, 那么Qt4要如何结束线程呢?

  • 首先须要使用一个flag来标识线程的状态(执行仍是中止), 好比定义一个变量bool is_stopped 初值赋为false;
  • 而后本身写一个结束线程的函数, 好比stop(), 当调用my_thread->stop();时将is_stopped改成true;
  • run()中判断, 若是is_stoppedfalse线程继续执行, 若是为true线程退出; 别忘了退出前再将is_stopped改成false, 否则线程无法再次开启了。

代码以下:

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QDebug>

class MyThread : public QThread
{
    Q_OBJECT

public:
    MyThread();
    void stop();  // 添加stop()方法

private:
    volatile bool is_stopped;  // 添加标识变量

protected:
    void run();

signals:

public slots:


};

#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"

// 构造函数
MyThread::MyThread()
{
    is_stopped = false;  // 初始化标识变量
}

void MyThread::run()
{
    while (!is_stopped)  // 更改判断条件
    {
        qDebug() << "Running...";
        sleep(1);
    }

    qDebug() << "is_stopped is true, I'll exit.";
    is_stopped = false;  // 重置变量值
}

// 关闭线程
void MyThread::stop()
{
    is_stopped = true;
}

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    my_thread = new MyThread;  // 实例化
    my_thread->start();  // 开启线程


    // 主线程阻塞5秒
    QDateTime start = QDateTime::currentDateTime();
    QDateTime now;
    do {
        now = QDateTime::currentDateTime();
    } while(start.secsTo(now) < 5);


    // 关闭线程
    my_thread->stop();  // 用本身定义的方法关闭线程
    my_thread->wait();

}

MainWindow::~MainWindow()
{

}

附: exit()和requestInterruption()区别

看例子, 咱们修改一下run()函数和关闭线程部分的代码:

mythread.cpp

void MyThread::run()
{
    while (!isInterruptionRequested())
    {
        qDebug() << "Running...";
        sleep(1);
    }
    qDebug() << "子线程: 我只退出了while循环, 没有真正结束";

    exec();  // 事件循环
    qDebug() << "子线程: 我真的要结束了";
}

mainwindow.cpp

// 关闭线程
    my_thread->requestInterruption();
    qDebug() << "主线程: 发起中断请求";
    my_thread->wait(3000);

    my_thread->quit();
    qDebug() << "主线程: 请求退出线程的事件循环";
    my_thread->wait();  // 等待线程结束

运行结果:

在主进程requestInterruption()后, 只是使得isInterruptionRequested()变为true, 退出了while循环, 在主线程中调用wait(3000), 并无马上返回, 而是3秒超时后才返回, 说明子线程没有真正结束, 而是执行到了exec()处进行事件循环。经过调用quit()exit()来结束子线程的事件循环, 子线程才真的结束了。

3.2 moveToThread方式(Qt5新增 官方推荐)

a. 定义一个继承QObject的类

  • 首先, 建立一个类并继承QObject, 把要在线程中执行的工做做为类的槽函数:

dowork.h

#ifndef DOWORK_H
#define DOWORK_H

#include <QObject>
#include <QDateTime>
#include <QDebug>

class DoWork : public QObject
{
    Q_OBJECT
public:
    explicit DoWork(QObject *parent = nullptr);

public slots:
    void do_something();
};

#endif // DOWORK_H

dowork.cpp

#include "dowork.h"

DoWork::DoWork(QObject *parent) : QObject(parent)
{

}

void DoWork::do_something()
{
    int a = 5;
    while(a--)
    {
        qDebug() << "Doing something ...";
        QDateTime start = QDateTime::currentDateTime();
        QDateTime now;
        do {
            now = QDateTime::currentDateTime();
        } while(start.secsTo(now) < 1);
    }
    qDebug() << "Done";
}

b. moveToThread

  • 而后, 建立一个线程对象, 把work1对象移到新线程下:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QThread>
#include "dowork.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    DoWork *work1;  // 自定义的类
    QThread *new_thread;  // 新线程
};

#endif // MAINWINDOW_H

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 实例化
    work1 = new DoWork;
    new_thread = new QThread;
    
    work1->moveToThread(new_thread);  // 搬到线程下
}

c. 启动线程

  • 绑定线程启动后要作的工做:

    connect(new_thread, &QThread::started, work1, &DoWork::do_something);

    使用moveToThread的方法很是灵活, 你不必定要用&QThread::started来触发do_something, 也可使用自定义的信号, 为了例程简单明了, 这里不举例了。

  • 启动线程

    new_thread->start();

d. 结束后的清理工做

  • 为了更安全, 线程结束后别忘了释放资源:

    connect(new_thread, &QThread::finished, work1, &QObject::deleteLater);
    MainWindow::~MainWindow()
    {
        new_thread->requestInterruption();
        new_thread->quit();
        new_thread->wait();
    }

附: mainwindow.cpp 完整代码

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 实例化
    work1 = new DoWork;
    new_thread = new QThread;

    work1->moveToThread(new_thread);  // 搬到线程下

    connect(new_thread, &QThread::started, work1, &DoWork::do_something);
    connect(new_thread, &QThread::finished, work1, &QObject::deleteLater);

    new_thread->start();
}

MainWindow::~MainWindow()
{
    new_thread->requestInterruption();
    new_thread->quit();
    new_thread->wait();
}

运行结果以下:

相关文章
相关标签/搜索