文章目录

  • 1 Qt中另一种创建线程的方式
    • 1.1 另一种创建线程的方式
    • 1.2 同步型线程的设计
    • 1.3 异步型线程的设计

1 Qt中另一种创建线程的方式

1.1 另一种创建线程的方式

历史的痕迹:

现代软件架构技术中的参考准则:

  • 尽量使用组合的方式实现系统功能。
  • 代码中仅体现需求中的集成关系。

思考:

  • 通过集成的方式实现新的线程类有什么实际意义?

事实上:

结论:

  • 通过继承的方式实现多线程没有任何实际意义。
  • QThread对应于操作系统中的线程。
  • QThread用于充当一个线程操作的集合。
  • 应该提供灵活的方式指定线程入口函数。
  • 尽量避免重写void run()。

在新版本的QThread类的实现中:

问题:

  • 如何灵活的指定一个线程对象的线程入口函数?

解决方案-信号与槽:

  1. 在类中定义一个槽函数void tmain()作为线程入口函数。
  2. 在类中定义一个QThread成员对象m_thread。
  3. 改变当前对象的线程依附性到m_thread。
  4. 连接m_thread的start()信号到tmain()(一定不要忘了在tmain中调用quit退出线程)。

1.2 同步型线程的设计

AnotherThread.h:

#ifndef ANOTHERTHREAD_H
#define ANOTHERTHREAD_H#include <QObject>
#include <QThread>class AnotherThread : public QObject
{Q_OBJECTQThread m_thread;
protected slots:void tmain();
public:explicit AnotherThread(QObject *parent = 0);void start();void terminate();void exit(int c);~AnotherThread();};#endif // ANOTHERTHREAD_H

AnotherThread.cpp:

#include "AnotherThread.h"
#include <QDebug>AnotherThread::AnotherThread(QObject *parent) :QObject(parent)
{moveToThread(&m_thread);connect(&m_thread, SIGNAL(started()), this, SLOT(tmain()));
}void AnotherThread::tmain()
{qDebug() << "void AnotherThread::tmain() tid = " << QThread::currentThreadId();for(int i=0; i<10; i++){qDebug() << "void AnotherThread::tmain() i = " << i;}qDebug() << "void AnotherThread::tmain() end";m_thread.quit();
}void AnotherThread::start()
{m_thread.start();
}void AnotherThread::terminate()
{m_thread.terminate();
}void AnotherThread::exit(int c)
{m_thread.exit(c);
}AnotherThread::~AnotherThread()
{m_thread.wait();
}

main.cpp:

#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QThread>
#include "AnotherThread.h"void test()
{AnotherThread at;at.start();
}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);qDebug() << "main() tid = " << QThread::currentThreadId();test();return a.exec();
}

1.3 异步型线程的设计

核心要点是在线程结束后调用deletLater函数。

AnotherThread.h:

#include "AsyncThread.h"
#include <QDebug>AsyncThread::AsyncThread(QObject *parent) :QThread(parent)
{}void AsyncThread::run()
{for (int i=0; i<5; i++){qDebug() << "MyThread run() : " << i << ", thread id = " << currentThreadId();sleep(1);}deleteLater();
}bool AsyncThread::event(QEvent *e)
{qDebug() << "void AsyncThread::event(QEvent *)";return QThread::event(e);
}int AsyncThread::exec()
{qDebug() << "int AsyncThread::exec()";return QThread::exec();
}AsyncThread::~AsyncThread()
{qDebug() << "AsyncThread::~AsyncThread() : " << currentThreadId();
}AsyncThread *AsyncThread::NewInstance()
{return new AsyncThread;
}

AnotherThread.cpp:

#include "AnotherThread.h"
#include <QDebug>AnotherThread::AnotherThread(QObject *parent) :QObject(parent)
{m_pThread = QThread::currentThread();moveToThread(&m_thread);connect(&m_thread, SIGNAL(started()), this, SLOT(tmain()));
}AnotherThread::~AnotherThread()
{if (m_thread.isRunning()){m_thread.wait();}qDebug() << "AnotherThread::~AnotherThread() : " << QThread::currentThreadId();
}AnotherThread *AnotherThread::NewInstance()
{return new AnotherThread;
}void AnotherThread::tmain()
{for (int i=0; i<5; i++){qDebug() << "void AnotherThread::tmain() : " << i << ", thread id = " << QThread::currentThreadId();}m_thread.quit();moveToThread(m_pThread);deleteLater();
}void AnotherThread::onFinished()
{qDebug() << "void AnotherThread::finished()";//delete this;
}void AnotherThread::start()
{m_thread.start();
}void AnotherThread::exit(int c)
{m_thread.exit(c);
}void AnotherThread::quit()
{m_thread.quit();
}bool AnotherThread::wait(unsigned long t)
{return m_thread.wait(t);
}void AnotherThread::terminate()
{m_thread.terminate();
}

参考资料:

  1. QT实验分析教程

Qt中另一种创建线程的方式相关推荐

  1. 除了Thread和Runnable,你还知道第三种创建线程的方式Callable吗

    相信大多数学过多线程的同学都知道创建线程常见的有三种方式,一种是继承Thread类,一种是实现Runnable接口,最后一种就是Callable,今天主要是对最后不常见的Callable方式进行介绍. ...

  2. java创建线程的方式到底有几种?(详解)

    创建线程的方式到底有几种? 一,创建多线程的方式 1,官方解释 2,实现Runnable接口 3,继承Thread类 3,二者区别 3.1,本质区别 3.2,优先考虑使用第一种 二,误以为是创建线程的 ...

  3. 在Qt中使用C++代码创建界面

    好儿郎~志在四方 Qt视频教程地址:http://space.bilibili.com/84360636/#!/index 目录视图 摘要视图 订阅 图灵赠书--程序员11月书单    [思考]Pyt ...

  4. 三种创建线程方式之Callable接口

    一.类继承关系及API解析 Callable接口 @FunctionalInterface public interface Callable<V> {V call() throws Ex ...

  5. 想不到吧,Java创建线程的方式只有一种

    目录 前言 继承Thread方式 实现Runnable接口 实现callable接口 总结 前言 看到这个标题的小伙伴先别着急喷我--在面试的时候,我们经常会被问到这种基础题:Java创建线程的方式有 ...

  6. java 创建线程的方式

    Java创建线程的方式 1 官方API里面的说法 通过官方 API 可以发现创建线程的方式有两种,第一种是通过继承 Thread 类,重写 run 方法:第二种是通过实现 Runnable 接口,创建 ...

  7. Java基础_17 | Java多线程程序设计(Java中两种创建线程的方法、多线程之间的同步和互斥)

    1. 多线程实现最核心的机制 一个程序在其执行过程中, 可以产生多个线程, 形成多条执行线索.,每条线程,有产生.存在和消亡的过程,并且独立完成各自的功能,互不干扰. 多线程程序运行只占用一个CPU, ...

  8. Executors-四种创建线程的手段

    1 Executors.newCachedThreadPool() 从构造方法可以看出,它创建了一个可缓存的线程池.当有新的任务提交时,有空闲线程则直接处理任务,没有空闲线程则创建新的线程处理任务,队 ...

  9. qt中opengl窗口的创建

    该笔记借鉴自 : "懂deeee珍惜"的 现代OpenGL+Qt学习笔记之二:程序框架 "爱种鱼的猫"的 QT中使用OpenGL(0)--创建一个窗口 引用引自 ...

最新文章

  1. tensorflow 回归的例子,包括保存模型和重新预测
  2. OpenStack 2018 年终盘点
  3. 进阶学习(3.13) Proxy Pattern 代理模式
  4. “机器换人”没什么可抱怨
  5. python 浮点数精度丢失_javascript解决小数的加减乘除精度丢失的方案
  6. python单行注释和多行注释
  7. 人类史上20个“最强大脑”
  8. Java基础学习总结(97)——合格的Java的架构师需要具备的技术知识
  9. 17.卷1(套接字联网API)--- ioctl 操作
  10. 典型环节的电路模拟MATLAB,典型环节的模拟及参数测试
  11. Ubuntu下Jlink驱动安装使用
  12. Word中文字后面是白色的
  13. 2020年的5种常见骇客行为,你的电脑安全吗?
  14. 十二烷基硫酸钠(SDS)将Fe3O4磁性纳米粒子定量地修饰到多壁碳纳米管|化学试剂
  15. 张量(Tensor)的降维与升维
  16. Android 10.0锁屏界面默认不显示Notification通知
  17. 【面试宝典】准备面试了~集合
  18. 如何改变php的语言变中文,如何使php将unicode转换中文
  19. 学习记录:关于Uniapp与Java实现支付宝沙箱APP内支付
  20. 松下FP XH六轴标准程序,程序控制六个伺服,轴的点动控制

热门文章

  1. PCB 零件尺寸图:Arduino Mega 2560 尺寸
  2. PyTorch 实现经典模型7:YOLO (v1, v2, v3, v4)
  3. 2.10 局部最优的问题-深度学习第二课《改善深层神经网络》-Stanford吴恩达教授
  4. 2.5 导数-深度学习-Stanford吴恩达教授
  5. 2.4 梯度下降法-深度学习-Stanford吴恩达教授
  6. mysql 安装手册(转)
  7. Android4.0添加java层服务
  8. 点到点链路的滑动窗口协议
  9. linux下文件系统的启动过程
  10. C语言从0到1·源程序,源文件,目标文件之间的关系