QtConcurrent记录

等待返回处理后的结果

int function(const int &a)
{
   
    qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<QThread::currentThreadId();
    return a*100;
}
 QList<int> list{
   0,1,2,3};
   auto f  = QtConcurrent::blockingMapped(list,function);
   for (auto& v:f) {
   
       qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<v;
   }

返回处理后的结果

  QList<int> list{
   0,1,2,3};
    auto f  = QtConcurrent::mapped(list,function);
    f.waitForFinished();
    for (auto& a: f.results()) {
   
        qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<a;
    }

直接修改原队列

void function(int &a)
{
   
    qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<QThread::currentThreadId();
     a = a*100;
}
QList<int> list{
   0,1,2,3};
    auto f  = QtConcurrent::map(list,function);
    f.waitForFinished();
    for (auto& a: list) {
   
        qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<a;
    }

将结果继续传递给新函数,并在新函数里再处理成一个单值

新函数必须是“V function(T &result,const U &intermediate)”的形式

int function(const int &a)
{
   
    return  a*100;
}

void reduce(int& result,const int& in)
{
   
    result += in ;
}

void Widget::on_pushButton_clicked()
{
   
    QList<int> list{
   0,1,2,3};
    auto f  = QtConcurrent::mappedReduced(list,function,reduce);
    f.waitForFinished();
    qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<f.result();
}

可以使用成员函数

 QStringList lst;
    lst<<"123"<<"456"<<"789";
    auto f1 = QtConcurrent::blockingMappedReduced(lst,&QString::length,strLength);
    qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<f1;

输出

9
 auto f2 = QtConcurrent::blockingMappedReduced(lst,&QString::length,&QList<int>::push_back);

输出

(3, 3, 3)

支持函数对象

struct strExpand
{
   
    strExpand(int a):m_a{
   a}{
   }
    typedef QString result_type;//
    QString operator()(const QString& str)
    {
   
        return  str + QString::number(m_a);
    }
    int m_a{
   0};
};
 QStringList lst;
    lst<<"123"<<"456"<<"7899";
auto f3  = QtConcurrent::mapped(lst,strExpand{
   10086});
    f.waitForFinished();
    qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<f3.results();

bind

int fun(int b,const int& a)
{
   
    return  a+1+b;
}

QList<int> list{
   0,1,2,3};
    auto bFun = std::bind(&fun,100,std::placeholders::_1);
    auto f  = QtConcurrent::mappedReduced(list,bFun,reduce);
    f.waitForFinished();
    qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<f.result();

过滤

bool aselect(const int& value)
{
   
    return  value%2;
}
  auto f11  = QtConcurrent::filtered(list,aselect);
    f11.waitForFinished();

run

 QtConcurrent::run([]{
   
        qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<QThread::currentThreadId();
    });
  QFuture<bool> f =  QtConcurrent::run(this,&Widget::threadTest,1024);

QFutureWatcher 观察并发结束

 m_watcher.setFuture(f);
    qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__;
    connect(&m_watcher,&QFutureWatcherBase::finished,this,[&]{
   
        qDebug() << __LINE__ <<  __FUNCTION__ << __FILE__<<m_watcher.result();
    });

相关推荐

  1. QtConcurrent记录

    2024-01-03 15:36:04       39 阅读
  2. Qt线程之QtConcurrent的介绍

    2024-01-03 15:36:04       17 阅读
  3. 【QT】QtConcurrent的使用介绍,与std::thread的区别

    2024-01-03 15:36:04       17 阅读
  4. 【QT进阶】Qt线程与并发之QtConcurrent的简单介绍

    2024-01-03 15:36:04       15 阅读
  5. HSQL 记录

    2024-01-03 15:36:04       44 阅读
  6. bug 记录

    2024-01-03 15:36:04       38 阅读
  7. QT 记录

    2024-01-03 15:36:04       44 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-03 15:36:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-03 15:36:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-03 15:36:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-03 15:36:04       20 阅读

热门阅读

  1. 力扣42. 接雨水

    2024-01-03 15:36:04       35 阅读
  2. 算法:动态规划

    2024-01-03 15:36:04       40 阅读
  3. setFirstResult ,setMaxResults

    2024-01-03 15:36:04       33 阅读
  4. pip安装报错SSL

    2024-01-03 15:36:04       45 阅读
  5. 简易版前端项目离线方案-接口及页面离线缓存

    2024-01-03 15:36:04       37 阅读
  6. C++ gRPC helloworld 示例代码

    2024-01-03 15:36:04       39 阅读
  7. 数据结构OJ实验7-树结构及应用

    2024-01-03 15:36:04       31 阅读
  8. MongoDB聚合:$addField

    2024-01-03 15:36:04       37 阅读
  9. 大数据系列之:读取parquet文件统计数据量

    2024-01-03 15:36:04       36 阅读
  10. Mac 彻底删除 node 和 npm

    2024-01-03 15:36:04       37 阅读
  11. 详解汇编cll ret push pop 并附源码

    2024-01-03 15:36:04       43 阅读
  12. MySQL5.7更新的内容

    2024-01-03 15:36:04       33 阅读
  13. 微服务(12)

    2024-01-03 15:36:04       36 阅读