C++11消息队列 + Qt线程池 + QRunnable执行任务简单模型

匿名 (未验证) 提交于 2019-12-02 23:42:01

1、模板类queue,包含头文件<queue>中,是一个FIFO队列。

queue.push():在队列尾巴增加数据 queue.pop():移除队列头部数据 queue.font():获取队列头部数据的引用...

2、Qt库的线程池,QThreadPool

QThreadPool.setMaxThreadCount():设置线程池最大线程数 QThreadPool.start(new QRunnable(..)):开启线程池调用QRunnable

3、QRunnable执行任务

void run();//重写虚函数,在里面消费任务队列 setAutoDelete(true)//默认就是true,消费结束自动回收内存

4、代码

run.h

#ifndef RUN_H #define RUN_H  #include <QObject> #include <QRunnable> #include <string> #include <iostream>  struct MyString {     std::string valueStr; };  class Run : public QObject , public QRunnable {     Q_OBJECT public:     Run() = default;     Run(const MyString& myString); protected:     ~Run() = default;     void run(); signals:  public slots:  private:     MyString myString; };  #endif // RUN_H说明:MyString结构体代替实际项目中的任务,Run接口的run纯虚函数用来消费分配来的MyString
run.cpp
#include "run.h" #include <QThread> #include <QDebug> Run::Run(const MyString &myString) {     this->myString = myString;     //this->setAutoDelete(true);//默认就是true }  void Run::run() {     //std::cout << "value:" << this->myString.valueStr <<";调用线程ID为:" << QThread::currentThread() << std::endl;     qDebug() << "value:" << QString::fromStdString(myString.valueStr) << "thread:" << QThread::currentThreadId();     QThread::msleep(100); }说明:不使用cout打印是因为,cout打印不是原子操作,可能多个字符串被杂糅在一起打印;qDebug不会,应该底层加了锁

main.cpp

#include <QCoreApplication> #include "run.h" #include <queue> #include <mutex> #include <QThreadPool> #include <thread> using namespace std; queue<MyString> myList; mutex myMutex; volatile bool addThreadIsEnd = false; void makeListThread(); int main(int argc, char *argv[]) {        QCoreApplication a(argc, argv);     cout << "begin main" << endl;     thread addThread(makeListThread);     addThread.detach();     cout << "begin addThread" << endl;     QThreadPool tp;     tp.setMaxThreadCount(20);     cout << "begin threadPool" << endl;     while(true)     {         if(!myList.empty())         {             MyString tempMyString = myList.front();             tp.start(new Run(tempMyString));             myMutex.lock();             myList.pop();             myMutex.unlock();         }         else         {             if(addThreadIsEnd)             {                 break;             }             else             {                 QThread::msleep(10);             }         }     }     cout << "end main,list size:" << myList.size() << endl;     return a.exec(); }  void makeListThread() {     string a;     MyString tempMyString;     for(int i=0;i<10000;i++)     {         QThread::msleep(0);         a = to_string(i);         tempMyString.valueStr = a;         myMutex.lock();         myList.push(tempMyString);         myMutex.unlock();     }     addThreadIsEnd = true;     cout << "end addThread" << endl; }
5、模型

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!