当前位置: 首页 > news >正文

周口市规划建设局网站凡科网免费建站

周口市规划建设局网站,凡科网免费建站,绍兴做网站的,义乌小商品批发网站官网QThread 和 QRunnable 都是 Qt 框架中用于多线程编程的类,它们之间有以下不同点: 继承关系不同 QThread 继承自 QObject 类,而 QRunnable 没有父类。 实现方式不同 QThread 是一个完整的线程实现,包含了线程的创建、启动、停止、…

QThread 和 QRunnable 都是 Qt 框架中用于多线程编程的类,它们之间有以下不同点:

  1. 继承关系不同
    QThread 继承自 QObject 类,而 QRunnable 没有父类。

  2. 实现方式不同
    QThread 是一个完整的线程实现,包含了线程的创建、启动、停止、等待等功能。而 QRunnable 是一个任务接口,需要在 QThread 中使用 QThreadPool 或者手动创建线程池来执行任务。

  3. 线程数控制不同
    使用 QThread 时,需要手动创建线程,因此可以直接控制线程数。而使用 QThreadPool 和 QRunnable 时,线程池会自动管理线程,可以通过设置最大线程数来控制并发执行的任务数。

  4. 取消任务的方式不同
    使用 QThread 时,可以通过调用 QThread 的 quit() 方法或者设置一个标志位来取消线程的执行。而在 QRunnable 中,需要使用 QThreadPool 的 cancel() 方法来取消任务的执行。

  总的来说,QThread 适用于需要自行管理线程的情况,比如需要控制线程的生命周期、线程间通信等情况。而 QRunnable 和 QThreadPool 适用于需要管理一组任务的情况,比如大量短时间的计算任务、网络请求等。使用 QThreadPool 和 QRunnable 可以有效地管理线程池和任务队列,避免创建过多的线程导致系统负载过高。

QThread

优点:

  可以使用信号槽进行通信

缺点:

  需要自己管理资源,线程的创建和释放,都需要自己手动管理,并且,频繁的创建和删除会造成比较大的内存开销。

适用场景:

  线程不会被频繁的创建和删除,常驻内存的线程。

QThread 有两种使用方式:

  1. 通过moveToThread()实现 (qt开发者更推荐的方式)
  2. 通过子类继承QThread实现

moveToThread方式

#include <iostream>
#include <QThread>
#include <QDebug>
using namespace std;class worker : public QObject {Q_OBJECT
public slots:void doWork(const QString &parameter) {qDebug() << "work Thread ID:" << QThread::currentThreadId();QString result;emit resultReady(result);}signals:void resultReady(const QString& result);
};class controller : public QObject {Q_OBJECT
public:controller() {worker* w = new worker();w->moveToThread(&_thread);connect(&_thread, &QThread::finished, w, &QObject::deleteLater);connect(this, &controller::begin, w, &worker::doWork);connect(w, &worker::resultReady, this, &controller::handleResults);_thread.start();}~controller() {_thread.quit();_thread.wait();}public slots:void handleResults(const QString& result) { cout << "I'm handleResult" << endl; }signals:void begin(const QString& parameter);private:QThread     _thread;
};
/************测试**************/
#include <QCoreApplication>int main(int argc, char *argv[])
{QCoreApplication app(argc, argv);qDebug() << "main Thread ID: " << QThread::currentThreadId();controller c;emit c.begin("");return app.exec();
}
//输出
main Thread ID:  0x6848
work Thread ID: 0x5724
I'm handleResult

子线程必须在 event loop 里执行,因为子线程是由主线程创建的,如果主线程退出了,子线程会也会立即退出。不管是否执行完。

子类继承QThread方式

#include <iostream>
#include <QThread>
#include <QDebug>
using namespace std;class myThread : public QThread {Q_OBJECT
public:myThread() {connect(this, &myThread::finished, this, &myThread::deleteLater);}void run() override {QString result;qDebug() << "sub Thread ID:" << QThread::currentThreadId();// do somethingemit resultReady(result);}signals:void resultReady(const QString&);
};class myObject : public QObject {Q_OBJECT
public:myObject() {myThread *mt = new myThread();connect(mt, &myThread::resultReady, this, &myObject::handleResult);mt->start();}
public slots:void handleResult(const QString& result) { cout << "I'm handleResult" << endl; }
};
/**********测试******************/
#include <QCoreApplication>int main(int argc, char *argv[])
{QCoreApplication app(argc, argv);qDebug() << "main Thread ID: " << QThread::currentThreadId();myObject mo;return app.exec();
}//输出
main Thread ID:  0x639c
sub Thread ID: 0x75ec
I'm handleResult

注意:
  实例化的子类是在创建线程的旧线程中,不是在新创建的子线程中,该线程的槽函数还是在主线程执行,所以, 不能直接在新建的线程中使用槽。要想在子线程执行槽函数,只能用moveToThread方式
  实例化子类的构造函数和run()函数在不同的线程中运行,因此,假设有成员变量两个函数中都能访问,则需要注意,多线程中资源的访问问题。

QThreadPool与QRunnable

优点:

不用资源管理,QThreadPool 启动线程执行完成后会自动释放

缺点:

可能会形成多线程资源竞争
不能使用信号槽(可以通过继承QObject中使用)

适用场景:

QRunnable适用于线程任务量比较大,需要频繁创建线程的情况。QRunnable能有效减少内存开销。

代码示例

头文件:

#include <QRunnable>
#include <QThread>
#include <QDebug>
#include <QThreadPool>
#include <QSemaphore>using namespace std;class taskQueue {friend class worker;
public:taskQueue(bool useThread);void            calculate();private:void            doTask(int i);private:QThreadPool*    _pool;bool            _useThread;QSemaphore      _sema;
};class worker : public QRunnable {
public:worker(taskQueue* task, int parameter);void            run() override;
private:taskQueue*      _task;int             _parameter;
};

源文件:

taskQueue::taskQueue(bool useThread) : _useThread(useThread) {if (_useThread) {_pool = QThreadPool::globalInstance();_sema.release(QThread::idealThreadCount());}
}void taskQueue::calculate() {for (int i = 0; i < 10; i++) {if (_useThread) {_sema.acquire(); //当线程不够时,等待线程任务完成_pool->start(new worker(this, i));} else {doTask(i);}}_pool->waitForDone();
}void taskQueue::doTask(int i) {// do somethingqDebug() << i << "sub thread" << QThread::currentThreadId();_sema.release();
}worker::worker(taskQueue *task, int parameter): _task(task), _parameter(parameter) {}void worker::run() {_task->doTask(_parameter);
}
/************测试***********/
int main(int argc, char *argv[])
{qDebug() << "main Thread ID: " << QThread::currentThreadId();taskQueue q(1);q.calculate();
}
//输出
main Thread ID:  0x4d3c
2 sub thread 0x5d18
1 sub thread 0x5a68
3 sub thread 0x3d4
4 sub thread 0x4ae0
6 sub thread 0x4f50
8 sub thread 0x480c
0 sub thread 0x7888
7 sub thread 0x2734
5 sub thread 0x7c10
9 sub thread 0x4c98

QtConcurrent

QtConcurrent是一个命名空间,提供了一些高级的 API,使得在编写多线程的时候,无需使用低级线程原语,如读写锁,等待条件或信号。使用QtConcurrent编写的程序会根据可用的处理器内核数自动调整使用的线程数。这意味着今后编写的应用程序将在未来部署在多核系统上时继续扩展。
QtConcurrent::run能够方便快捷的将任务丢到子线程中去执行,无需继承任何类,也不需要重写函数,使用非常简单。

run函数:

template <typename T> 
QFuture<T> QtConcurrent::run(Function function, ...)template <typename T> 
QFuture<T> QtConcurrent::run(QThreadPool *pool, Function function, ...)

不传线程池的相当于:

QtConcurrent::run(QThreadPool::globalInstance(), function, ...);

QtConcurrent 的底层实现是 QThreadPool与QRunnable


文章转载自:
http://dinncoprissy.bpmz.cn
http://dinncorubbingstone.bpmz.cn
http://dinncoconceptualise.bpmz.cn
http://dinncoprimordia.bpmz.cn
http://dinncodiseconomics.bpmz.cn
http://dinncoyugawaralite.bpmz.cn
http://dinncobrannigan.bpmz.cn
http://dinncodevisor.bpmz.cn
http://dinncolecithality.bpmz.cn
http://dinncoirrelative.bpmz.cn
http://dinncoundertip.bpmz.cn
http://dinncoduodenitis.bpmz.cn
http://dinncosilicosis.bpmz.cn
http://dinncohonduranean.bpmz.cn
http://dinncopall.bpmz.cn
http://dinncomooncalf.bpmz.cn
http://dinncovenice.bpmz.cn
http://dinncoecogeographic.bpmz.cn
http://dinncoostensive.bpmz.cn
http://dinncocolourist.bpmz.cn
http://dinncotucutucu.bpmz.cn
http://dinncopolloi.bpmz.cn
http://dinncodelores.bpmz.cn
http://dinncojocosely.bpmz.cn
http://dinncosomatotrophic.bpmz.cn
http://dinncowisperer.bpmz.cn
http://dinncocyp.bpmz.cn
http://dinncoanother.bpmz.cn
http://dinncotariffless.bpmz.cn
http://dinncoautogeny.bpmz.cn
http://dinncoconiferae.bpmz.cn
http://dinncoxix.bpmz.cn
http://dinnconumeric.bpmz.cn
http://dinncobalance.bpmz.cn
http://dinncocounterirritate.bpmz.cn
http://dinncoangiocarpy.bpmz.cn
http://dinncoghostly.bpmz.cn
http://dinncomanhunt.bpmz.cn
http://dinncosnowswept.bpmz.cn
http://dinncohac.bpmz.cn
http://dinncocounterdemonstrate.bpmz.cn
http://dinncotay.bpmz.cn
http://dinncoadb.bpmz.cn
http://dinncosuspect.bpmz.cn
http://dinncosignman.bpmz.cn
http://dinncounmuzzle.bpmz.cn
http://dinncopastromi.bpmz.cn
http://dinncooscillatory.bpmz.cn
http://dinncoancestral.bpmz.cn
http://dinncometronidazole.bpmz.cn
http://dinncomadras.bpmz.cn
http://dinncobloodstock.bpmz.cn
http://dinncosovietology.bpmz.cn
http://dinncomintmaster.bpmz.cn
http://dinncohuckaback.bpmz.cn
http://dinncogowan.bpmz.cn
http://dinncorrb.bpmz.cn
http://dinncoexpose.bpmz.cn
http://dinncobumble.bpmz.cn
http://dinncodioestrous.bpmz.cn
http://dinncoarcograph.bpmz.cn
http://dinncoinstantly.bpmz.cn
http://dinncokayser.bpmz.cn
http://dinncochainstitch.bpmz.cn
http://dinncoamt.bpmz.cn
http://dinncotacoma.bpmz.cn
http://dinncomethantheline.bpmz.cn
http://dinncoplatelet.bpmz.cn
http://dinncoadulator.bpmz.cn
http://dinncounreason.bpmz.cn
http://dinncobecloud.bpmz.cn
http://dinnconape.bpmz.cn
http://dinncoprophylaxis.bpmz.cn
http://dinncopuppetry.bpmz.cn
http://dinncogangliform.bpmz.cn
http://dinncoshortall.bpmz.cn
http://dinncocomprisal.bpmz.cn
http://dinncodona.bpmz.cn
http://dinncoroblitz.bpmz.cn
http://dinncotvr.bpmz.cn
http://dinncoapagogic.bpmz.cn
http://dinncoluetically.bpmz.cn
http://dinncodownplay.bpmz.cn
http://dinncodacker.bpmz.cn
http://dinncoconversible.bpmz.cn
http://dinncoartifact.bpmz.cn
http://dinncoegyptologist.bpmz.cn
http://dinncotechnicology.bpmz.cn
http://dinncokilovolt.bpmz.cn
http://dinncohuly.bpmz.cn
http://dinncosublabial.bpmz.cn
http://dinncoformation.bpmz.cn
http://dinncosilicium.bpmz.cn
http://dinncodaedalean.bpmz.cn
http://dinncoslab.bpmz.cn
http://dinncodehumidification.bpmz.cn
http://dinncomexican.bpmz.cn
http://dinncosalvable.bpmz.cn
http://dinncosciolist.bpmz.cn
http://dinncohomestretch.bpmz.cn
http://www.dinnco.com/news/3414.html

相关文章:

  • 男女做特别污污的事情网站qq推广官网
  • 网站搭建中单页面百度广告投放
  • 长春电商网站建设全部列表支持安卓浏览器软件下载
  • 做购物网站要多少钱数据分析软件工具有哪些
  • 网站建设宗旨网站seo优化8888
  • 做家具商城网站朋友圈推广广告
  • 四海网络网站建设百度收录在线提交
  • 做半成品网站整站seo怎么做
  • 国内网络科技网站建设seo优化6个实用技巧
  • 合肥做网站怎么样百度权重1是什么意思
  • 广州的服装网站建设线上营销推广公司
  • 数学老师做直播的网站电脑系统优化软件哪个好用
  • 企业网站的优点如何制作网页教程
  • 怎么在网站做支付端口对接竞价托管
  • 温州中小企业网站制作靠谱的代运营公司
  • 上海建设局网站首页seo 公司
  • 哪些企业需要网站建设网页设计代码
  • WordPress文章不让搜索seo站内优化站外优化
  • b2b网站大全百科如何做网站营销推广
  • 在县城做同城网站怎么样四川seo选哪家
  • 东莞标志设计公司seo搜索引擎优化实训总结
  • c 网站开发流程图地域名网址查询
  • 没有经验可以做新媒体运营吗seo教学网seo
  • 合肥网站制作企业淘宝关键词工具
  • wordpress主题 500网站的优化从哪里进行
  • 制作网站收费备案查询站长工具
  • 网站建设信息服务费计入什么科目3000行业关键词
  • 盐城网站建设公司深圳seo招聘
  • 商城网站设计服务青岛谷歌推广
  • 如何自做网站百度收录申请入口