• QT blockingFilter blockingMap blockingMapped


    blockingFilter

    主要作用是筛选出符合条件的项值结果集,并与之替换原有序列列表

    blockingMap

    可以直接修改容器的每一项

    blockingMapped

    不直接修改容器的每一项,而是将处理后的结果返回一个新的容器

    blockingMappedReduced

    ResultType QtConcurrent::blockingMappedReduced(const Sequence &sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions = ReduceOptions(UnorderedReduce | SequentialReduce))
    
    Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction.
    Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions.
    
    • 1
    • 2
    • 3
    • 4

    按顺序为每个项目调用mapFunction一次。每个mapFunction的返回值被传递给reduceFunction。
    请注意,当mapFunction被并发调用时,一次只有一个线程会调用reduceFunction。reduceFunction的调用顺序由reduceOptions决定。

    注意:blockingFilter、blockingMap、blockingMapped、blockingMappedReduced 这些函数将阻塞直到序列中的所有项都被处理完。当然,他们也有异步版本(QtConcurrent::map、QtConcurrent::mapped、QtConcurrent::mappedReduced),只要把前面的block去掉就是对应的异步版本函数了。

    示例代码

    #include 
    #include 
    #include 
    
    int Addfunc(const int &arg)
    {
        return arg + 1;
    };
    
    int multiplyFunc(const int &num)
    {
        return num * 10;
    }
    
    void reducedFunction(int &result, const int &intermedia)
    {
        result += intermedia;
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        //<1>blockingFilter 主要作用是筛选出符合条件的项值结果集,并与之替换原有序列列表
        QList<int> list0{2, 7, 6, 5, 10, 9, 8, 3, 4};
        QtConcurrent::blockingFilter(list0, [](int v) { return v > 5; });
        qDebug() << "blockingFilter sequence=" << list0;//(7, 6, 10, 9, 8)
    
    
        //<2>blockingMap 可以直接修改容器的每一项
        QList<int> list1 = {1, 2, 3, 4, 5};
        QtConcurrent::blockingMap(list1, [](int &num){num = num * 10;});
        qDebug() << "blockingMap list1=" << list1;//(10, 20, 30, 40, 50)
    
    
        //<3>blockingMapped 不直接修改容器的每一项,而是将处理后的结果返回一个新的容器
        QList<int> list2 = {3, 4, 5, 6, 7};
        list2 = QtConcurrent::blockingMapped(list2, multiplyFunc);
        qDebug() << "blockingMapped list2=" << list2;//(30, 40, 50, 60, 70)
    
    
        //<4>按顺序为每个项目调用multiplyFunc一次,每个multiplyFunc的返回值被传递给reducedFunction
        QList<int> list3 = {6, 7, 8, 9, 10};
        int res = QtConcurrent::blockingMappedReduced(list3, multiplyFunc, reducedFunction);
        qDebug() << "blockingMappedReduced res=" << res;//400
    
    
        //--------------------------Asynchronous call--------------------------
    #if 1
        //QtConcurrent::map
        QList<int> sequence0{1, 2, 3, 4, 5, 6, 7};
        QFuture<void> future0 = QtConcurrent::map(sequence0, Addfunc);
        QFutureWatcher<void> *watcher = new QFutureWatcher<void>();
        QObject::connect(watcher, &QFutureWatcher<void>::finished, [=](){
            qDebug() << "QtConcurrent::map" << sequence0;
        });
    
        //QtConcurrent::mapped
        QList<int> sequence1{1, 2, 3, 4, 5, 6, 7};
        QFuture<int> future1 = QtConcurrent::mapped(sequence1, multiplyFunc);
        qDebug() << "QtConcurrent::mapped" << future1.results();//(10, 20, 30, 40, 50, 60, 70)
        //新的结果用QFuture::const_iterator或QFutureIterator进行访问
        QFutureIterator<int> itr(future1);
        while (itr.hasNext()) {
            qDebug() << "itr=" << itr.next();
        }
    
        //QtConcurrent::mappedReduced()类似于mapped()
        //区别在于将结果继续传递给一个新函数,并在新函数里再处理成一个单值
        QList<int> sequence2{1, 2, 3, 4, 5, 6, 7};
        QFuture<int> future2 = QtConcurrent::mappedReduced(sequence2, multiplyFunc, reducedFunction);
        future2.waitForFinished();
        int result = future2.result();
        qDebug() << "QtConcurrent::mappedReduced" << result;//280
    #endif
    
        return a.exec();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
  • 相关阅读:
    【OAuth2】十二、Spring Authorization Server的配置
    【Spring Boot】
    从零开始学C语言系列之番外篇《初学单片机的路》
    WPF上位机通信组件与Modbus协议
    自从面试了一个测试岗00后卷王,老油条感叹真干不过,但是...
    数据可视化之全球咖啡豆的产销:全球总产量平稳上升,预计今年总产量约1.75亿袋
    [ vulhub漏洞复现篇 ] Thinkphp SQL注入 && 敏感信息泄露
    SVN目录中空格引起的权限异常问题?
    慌了,面试官问我G1垃圾收集器
    点云从入门到精通技术详解100篇-基于点云和图像的智能交通路侧感知(续)
  • 原文地址:https://blog.csdn.net/hellokandy/article/details/134260868