QT创建新线程的方法:
1、继承QThread的多线程方法 (run函数)
2、继承QObject的多线程方法(类转移)
一、继承QThread的多线程方法(run函数)
在使用继承QThread的run方法之前需要了解一条规则:
QThread只有run函数是在新线程里的,其他所有函数都在QThread生成的线程里
关于继承一个类,大家应该都非常熟悉,所以这里只贴出代码(未上锁和上锁):
#include <QCoreApplication>
#include<QDebug>
#include<QTextStream>
#include<QThread>
#include<QMutex>
class MyThreadA : public QThread
{
public:
virtual void run();
};
class MyThreadB : public QThread
{
public:
virtual void run();
};
int number=6;
QMutex mutex;
void MyThreadA::run()
{
mutex.lock();
number*=5;
sleep(1);
number/=4;
mutex.unlock();
}
void MyThreadB::run()
{
mutex.lock();
number*=3;
sleep(1);
number/=2;
mutex.unlock();
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
MyThreadA a;
MyThreadB b;
a.start();
b.start();
a.wait();
b.wait();
qDebug()<<number;
return app.exec();
}
//上锁输出10
//不上锁输出11
二、继承QObject的多线程方法(类转移)
参考Qt5.9继承QObject创建多线程实例(第二种方法)
利用继承QObject方法创建多线程,主要的步骤有一下几点:(注意:退出线程循环后,还要调用QThread::quit()函数,该线程才会触发QThread::finished()信号)
1、创建一个类MyThread,基类是QObject。
2、在类MyThread中创建一个槽函数,用于运行多线程里的代码。所以耗时代码全部在这个槽函数中运行。
3、实例一个QThread线程对象(容器),将类MyThread的实例对象转到该容器中,用函数QObject::moveToThread(QThread *thread);
myObjectThread->moveToThread(firstThread);
4、用一个信号触发该多线程槽函数,比如用QThread::started()信号
connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot()));
5、用·信号QThread::finished绑定槽函数QThread::deleteLatater(),在线程退出时销毁该线程并释放资源。
connect(firstThread,SIGNAL(finished()),firstThread,SLOT(deleteLater()));
6、所有线程初始化完成时,启动函数QThread::start()开启多线程,然后自动触发多线程启动信号QTread::started()。
具体代码如下:
新建一个widget工程,不用勾选ui界面。同时添加类MyThread。
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
class MyThread : public QObject
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
void closeThread();
signals:
public slots:
void startThreadSlot();
private:
volatile bool isStop;
};
#endif // MYTHREAD_H
mythread.cpp
#include "mythread.h"
#include<QDebug>
#include<QThread>
MyThread::MyThread(QObject *parent) : QObject(parent)
{
isStop=false;
}
void MyThread::closeThread()
{
isStop=true;
}
void MyThread::startThreadSlot()
{
while(1)
{
if(isStop)
{
return;
}
qDebug()<<"MyThread::startThreadSlot QThread:::currentThreadId()=="<<QThread::currentThreadId();
QThread::sleep(1);
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QVBoxLayout>
#include<QThread>
#include"mythread.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void createView();
private slots:
void openThreadSlot();
void closeThreadSlot();
void finishedThreadSlot();
private:
QVBoxLayout *mainLayout;
QThread *firstThread;
MyThread *myObjectThread;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include<QDebug>
#include<QPushButton>
#include<QThread>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
createView();
}
Widget::~Widget()
{
}
void Widget::createView()
{
//UI界面
mainLayout=new QVBoxLayout(this);
QPushButton *openThreadBtn=new QPushButton("打开线程");
QPushButton *closeThreadBtn=new QPushButton("关闭线程");
mainLayout->addWidget(openThreadBtn);
mainLayout->addWidget(closeThreadBtn);
mainLayout->addStretch();
connect(openThreadBtn,SIGNAL(clicked(bool)),this,SLOT(openThreadSlot()));
connect(closeThreadBtn,SIGNAL(clicked(bool)),this,SLOT(closeThreadSlot()));
}
void Widget::openThreadSlot()
{
//开启一个线程
qDebug()<<"开启线程";
firstThread=new QThread;
myObjectThread=new MyThread;
myObjectThread->moveToThread(firstThread);
connect(firstThread,SIGNAL(started()),myObjectThread,SLOT(startThreadSlot()));
connect(firstThread,SIGNAL(finished()),myObjectThread,SLOT(deleteLater()));
connect(firstThread,SIGNAL(finished()),this,SLOT(finishedThreadSlot()));
firstThread->start();
qDebug()<<"mainWidget QThread::currentThreadID()=="<<QThread::currentThreadId();
}
void Widget::closeThreadSlot()
{
//关闭一个线程
qDebug()<<"关闭线程";
if(firstThread->isRunning())
{
myObjectThread->closeThread();
firstThread->quit();
firstThread->wait();
}
}
void Widget::finishedThreadSlot()
{
qDebug()<<"多线程触发了finished信号123";
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(300,200);
w.show();
return a.exec();
}