提交 e0797973 编写于 作者: mahuifa's avatar mahuifa

feat:增加QThreadPool::run使用示例

上级 9cc0982a
......@@ -13,3 +13,4 @@ TEMPLATE = subdirs
SUBDIRS += UseQThread1 # Qt通过【实例化】QThread,使用moveToThread将QObject子类槽函数在子线程中执行
SUBDIRS += UseQThread2 # Qt通过子类化,继承QThread,重写run实现子线程
SUBDIRS += UseQThreadPool # Qt使用线程池QThreadPool示例
SUBDIRS += UseConcurrent # Qt Concurent API使用示例
#---------------------------------------------------------------------------------------
# @功能:
# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit(也支持其它编译器)
# @Qt IDE D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator
#
# @开发者 mhf
# @邮箱 1603291350@qq.com
# @时间 2023-02-23 13:59:15
# @备注
#---------------------------------------------------------------------------------------
QT += core gui concurrent
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# 定义程序版本号
VERSION = 1.0.0
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
contains(QT_ARCH, i386){ # 使用32位编译器
DESTDIR = $$PWD/../bin # 程序输出路径
}else{
DESTDIR = $$PWD/../bin64 # 使用64位编译器
}
# msvc >= 2017 编译器使用utf-8编码
msvc {
greaterThan(QMAKE_MSC_VER, 1900){ # msvc编译器版本大于2015
QMAKE_CFLAGS += /utf-8
QMAKE_CXXFLAGS += /utf-8
}else{
message(msvc2015及以下版本在代码中使用pragma execution_character_set("utf-8")】指定编码)
}
}
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QThread>
#include <QtConcurrent>
using namespace QtConcurrent;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle(QString("QtConcurrent::run使用示例--V%1").arg(APP_VERSION));
}
Widget::~Widget()
{
delete ui;
}
void test1()
{
qDebug() << "进入测试1函数";
QThread::sleep(3);
qDebug() << "离开测试1函数:" << QThread::currentThreadId();
}
void test2(QString value1, int value2)
{
qDebug() << "进入测试2函数:" << value1 <<" " << value2;
QThread::sleep(3);
qDebug() << "离开测试2函数:" << QThread::currentThreadId();
}
void Widget::test3()
{
qDebug() << "进入测试3函数";
QThread::sleep(3);
qDebug() << "离开测试3函数:" << QThread::currentThreadId();
}
void Widget::test4()
{
qDebug() << "进入测试4函数";
QThread::sleep(3);
qDebug() << "离开测试4函数:" << QThread::currentThreadId();
}
/**
* @brief 线程取自全局QThreadPool
*/
void Widget::on_pushButton_clicked()
{
QFuture<void> f1 = run(test1); // 在新的线程中异步执行无参全局函数
QFuture<void> f2 = run(test2, QString("传入线程的参数"), 123); // 在新的线程中异步执行有参全局函数(注意:参数2不能直接传字符串常量,需要传QString)
QFuture<void> f3 = run(&Widget::test3); // 在新的线程中异步执行静态成员函数
QFuture<void> f4 = run(this, &Widget::test4); // 在新的线程中异步执行成员函数
QFuture<void> f5 = run([]() // 在新线程中执行Lambda表达式
{
qDebug() << "进入测试Lambda表达式";
QThread::sleep(3);
qDebug() << "离开测试Lambda表达式:" << QThread::currentThreadId();
});
#if 1 // QFuture的用法
if (f1.isCanceled())
{
qDebug() << "并发操作已经取消";
}
if (f1.isFinished())
{
qDebug() << "并发操作已经完成";
}
if (f1.isRunning())
{
qDebug() << "并发操作正在执行";
}
if (f1.isPaused())
{
qDebug() << "并发操作已暂停";
}
if (f1.isStarted())
{
qDebug() << "并发操作已经启动";
}
#endif
}
int test5()
{
qDebug() << "进入测试5函数";
QThread::sleep(3);
qDebug() << "离开测试5函数:" << QThread::currentThreadId();
return 123;
}
/**
* @brief 等待线程执行完成,获取返回值
*/
void Widget::on_pushButton_2_clicked()
{
QFuture<int> f1 = run(test5); // 在新的线程中异步执行无参全局函数
int ret = f1.result(); // 等待线程结束,获取返回值(这个函数会阻塞当前线程)
qDebug() << "返回结果为:" << ret;
}
/**
* @brief 等待线程执行完成
*/
void Widget::on_pushButton_3_clicked()
{
QFuture<int> f1 = run(test5); // 在新的线程中异步执行无参全局函数
f1.waitForFinished(); // 等待线程结束(这个函数会阻塞当前线程)
qDebug() << "执行完成";
}
/**
* @brief 在指定线程池中运行
*/
void Widget::on_pushButton_4_clicked()
{
QFuture<void> f1 = run(&m_pool, test1); // 在新的线程中异步执行无参全局函数
QFuture<void> f2 = run(&m_pool, test2, QString("传入线程的参数"), 123); // 在新的线程中异步执行有参全局函数(注意:参数2不能直接传字符串常量,需要传QString)
QFuture<void> f3 = run(&m_pool, &Widget::test3); // 在新的线程中异步执行静态成员函数
QFuture<void> f4 = run(&m_pool, this, &Widget::test4); // 在新的线程中异步执行成员函数
QFuture<void> f5 = run(&m_pool, []() // 在新线程中执行Lambda表达式
{
qDebug() << "进入测试Lambda表达式";
QThread::sleep(3);
qDebug() << "离开测试Lambda表达式:" << QThread::currentThreadId();
});
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QThreadPool>
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
static void test3();
void test4();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
private:
Ui::Widget *ui;
QThreadPool m_pool;
};
#endif // WIDGET_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>447</width>
<height>304</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="pushButton">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="text">
<string>用法1,线程取自全局QThreadPool</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="pushButton_2">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="text">
<string>用法2,等待线程执行完成,获取返回值</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="pushButton_3">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="text">
<string>用法3,等待线程执行完成</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="pushButton_4">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="text">
<string>用法4,在指定线程池中运行</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
#---------------------------------------------------------------------------------------
# @功能: Qt Concurent API使用示例
# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit(也支持其它编译器)
# @Qt IDE D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator
#
# @开发者 mhf
# @邮箱 1603291350@qq.com
# @时间 2023-02-23 13:58:59
# @备注
#---------------------------------------------------------------------------------------
requires(qtHaveModule(concurrent)) # 如果条件为 falseqmake 在构建时会跳过此项目
TEMPLATE = subdirs
SUBDIRS += RunFunction
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册