先建立一个工程吧, 具体步骤前面讲过不少次了, 就再也不细说了。框架
而后在Header文件夹下建立添加一个头文件, 右键Headers -> Add New... -> C++ -> C++ Header File -> Choose函数
随便起个名字, 好比mythread, 而后点Next->Finish。ui
在mythread.h中包含QThread
头文件:操作系统
按住Ctrl键, 点击QThread
, 再按住Ctrl键点击qthread.h
进入到qthread.h文件, 源码就在这里了, 随便看看就好。哪里不懂就鼠标点一下不懂的地方, 而后按F1, 会跳转到相应的帮助文档,里面讲得很详细, 里面的英文也比较简单。线程
void start(Priority = InheritPriority);
经过调用start()
方法来启动线程,该方法会调用run()
函数(能够看到QThread中run()
为虚函数, 须要咱们来重载)。code
run()
函数可调用exec()
让该线程进入事件循环。orm
Priority为线程优先级(下面会讲)。对象
void exit(int retcode = 0);
void quit();
void terminate();
void requestInterruption();
+ bool isInterruptionRequested();
requestInterruption
用于请求线程进行中断。isInterruptionRequested
返回true/false, 用于判断是否有终止线程的请求。bool wait(unsigned long time = ULONG_MAX);
static void sleep(unsigned long);
static void msleep(unsigned long);
static void usleep(unsigned long);
bool isFinished() const;
bool isRunning() const;
bool isInterruptionRequested() const;
requestInterruption()
发出。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 | 使用和建立线程一样的优先级(这是默认值) |
void started(QPrivateSignal);
start
后, 执行run
前发出该信号。void finished(QPrivateSignal);
在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
新建一个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>
在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线程仍在运行, 直到线程被关闭。
上面咱们结束线程使用的是requestInterruption()
和isInterruptionRequested()
, 这是Qt5新增的, 那么Qt4要如何结束线程呢?
bool is_stopped
初值赋为false;stop()
, 当调用my_thread->stop();
时将is_stopped
改成true;run()
中判断, 若是is_stopped
为false
线程继续执行, 若是为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() { }
看例子, 咱们修改一下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()
来结束子线程的事件循环, 子线程才真的结束了。
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"; }
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); // 搬到线程下 }
绑定线程启动后要作的工做:
connect(new_thread, &QThread::started, work1, &DoWork::do_something);
使用moveToThread
的方法很是灵活, 你不必定要用&QThread::started
来触发do_something
, 也可使用自定义的信号, 为了例程简单明了, 这里不举例了。
启动线程
new_thread->start();
为了更安全, 线程结束后别忘了释放资源:
connect(new_thread, &QThread::finished, work1, &QObject::deleteLater);
MainWindow::~MainWindow() { new_thread->requestInterruption(); new_thread->quit(); new_thread->wait(); }
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(); }
运行结果以下: