zoukankan      html  css  js  c++  java
  • Qt之Concurrent Map和Map-Reduce

    简述

    QtConcurrent::map()、QtConcurrent::mapped()和QtConcurrent::mappedReduced()函数在一个序列中(例如:QList或QVector)的元素上并行地运行计算。QtConcurrent::map()直接修改一个序列,QtConcurrent::mapped()返回一个包含修改内容的新序列,QtConcurrent::mappedReduced()返回一个单独的结果。

    这些函数是Qt之Concurrent框架的一部分。

    上述每个函数都有一个blocking变量,其返回最终结果而不是一个QFuture。以和异步变量同样的方式来使用它们。

    QList<QImage> images = ...;
    
    // 每一个都调用blocks,直到整个操作完成
    QList<QImage> future = QtConcurrent::blockingMapped(images, scaled);
    
    QtConcurrent::blockingMap(images, scale);
    
    QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);

    注意:上述的结果类型不是QFuture对象,而是实际结果类型(在这种情况下,QList<QImage>和QImage)。

    Concurrent Map

    QtConcurrent::mapped()接受一个输入序列和map函数,该map函数被序列中的每个元素调用,返回一个包含map函数返回值的新序列。

    map函数必须是下面的形式:

    U function(const T &t);

    T和U可以是任意类型(它们甚至可以是同一类型),但是T必须匹配存储在序列中的类型,函数返回修改或映射的内容。

    下面示例介绍了如何为序列中的所有元素都应用scale函数:

    QImage scaled(const QImage &image)
    {
        return image.scaled(100, 100);
    }
    
    QList<QImage> images = ...;
    QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scaled);

    map的结果通过QFuture可用。查看QFuture和QFutureWatcher的文档,了解更多关于程序中如何使用QFutured的内容。

    如果想直接修改一个序列,使用QtConcurrent::map()。map函数必须是以下形式:

    U function(T &t);

    注意: map函数的返回值、返回类型没有被使用。

    使用QtConcurrent::map()和使用QtConcurrent::mapped()类似:

    void scale(QImage &image)
    {
        image = image.scaled(100, 100);
    }
    
    QList<QImage> images = ...;
    QFuture<void> future = QtConcurrent::map(images, scale);

    由于该序列被直接修改,QtConcurrent::map()不通过QFuture返回任何结果。然而,你仍然可以使用QFuture和QFutureWatcher监控map的状态。

    Concurrent Map-Reduce

    QtConcurrent::mappedReduced()类似于QtConcurrent::mapped(),但是返回一个新结果序列,通过一个reduce函数,结果被组合成一个值。

    reduce函数必须是以下形式:

    V function(T &result, const U &intermediate)

    T是最终结果的类型,U是map函数的返回类型。注意: reduce函数的返回值、返回类型并没有被使用。

    调用QtConcurrent::mappedReduced()如下所示:

    void addToCollage(QImage &collage, const QImage &thumbnail)
    {
        QPainter p(&collage);
        static QPoint offset = QPoint(0, 0);
        p.drawImage(offset, thumbnail);
        offset += ...;
    }
    
    QList<QImage> images = ...;
    QFuture<QImage> collage = QtConcurrent::mappedReduced(images, scaled, addToCollage);

    reduce函数将由map函数返回的每个结果调用一次,并且应该合并中间体到结果变量。QtConcurrent::mappedReduced()可以保证保证一次只有一个线程调用reduce,所以没有必要用一个mutex锁定结果变量。QtConcurrent::ReduceOptions枚举提供了一种方法来控制reduction完成的顺序。如果使用了QtConcurrent::UnorderedReduce(默认),顺序是不确定的;而QtConcurrent::OrderedReduce确保reduction按照原始序列的顺序完成。

    附加API功能

    使用迭代器而不是序列

    上述每个函数都有一个变量,需要一个迭代器范围,而不是一个序列。以和序列变量同样的方式来使用它们。

    QList<QImage> images = ...;
    
    QFuture<QImage> thumbnails = QtConcurrent::mapped(images.constBegin(), images.constEnd(), scaled);
    
    // map直接仅运行在non-const迭代器上
    QFuture<void> future = QtConcurrent::map(images.begin(), images.end(), scale);
    
    QFuture<QImage> collage = QtConcurrent::mappedReduced(images.constBegin(), images.constEnd(), scaled, addToCollage);

    Blocking变量

    上述每个函数能有一个blocking变量,其返回最终结果而不是一个QFuture。以和异步变量同样的方式来使用它们。

    QList<QImage> images = ...;
    
    // 每一个都调用blocks,直到整个操作完成
    QList<QImage> future = QtConcurrent::blockingMapped(images, scaled);
    
    QtConcurrent::blockingMap(images, scale);
    
    QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);

    注意:上述的结果类型不是QFuture对象,而是实际结果类型(在这种情况下,QList<QImage>和QImage)。

    使用成员函数

    QtConcurrent::map()、QtConcurrent::mapped()和QtConcurrent::mappedReduced()接受指向成员函数的指针,成员函数类类型必须匹配存储在序列中的类型:

    // 挤压所有的字符串到一个QStringList中
    QStringList strings = ...;
    QFuture<void> squeezedStrings = QtConcurrent::map(strings, &QString::squeeze);
    
    // 交换一个列表中的图片所有像素的rgb值
    QList<QImage> images = ...;
    QFuture<QImage> bgrImages = QtConcurrent::mapped(images, &QImage::rgbSwapped);
    
    // 创建一个列表中所有字符串长度的集合  
    QStringList strings = ...;
    QFuture<QSet<int> > wordLengths = QtConcurrent::mappedReduced(strings, &QString::length, &QSet<int>::insert);

    注意:当使用QtConcurrent::mappedReduced()时,你可以自由组合正常函数和成员函数的使用:

    // 可以使用QtConcurrent::mappedReduced()组合正常函数和成员函数
    
    // 计算字符串列表的平均长度
    extern void computeAverage(int &average, int length);
    QStringList strings = ...;
    QFuture<int> averageWordLength = QtConcurrent::mappedReduced(strings, &QString::length, computeAverage);
    
    // 创建一个列表中所有图片颜色分布的集合
    extern int colorDistribution(const QImage &string);
    QList<QImage> images = ...;
    QFuture<QSet<int> > totalColorDistribution = QtConcurrent::mappedReduced(images, colorDistribution, QSet<int>::insert);

    使用函数对象

    QtConcurrent::map()、QtConcurrent::mapped()和QtConcurrent::mappedReduced()接受函数对象,可用于添加状态函数调用。result_type typedef必须定义函数调用操作的结果类型:

    struct Scaled
    {
        Scaled(int size)
        : m_size(size) { }
    
        typedef QImage result_type;
    
        QImage operator()(const QImage &image)
        {
            return image.scaled(m_size, m_size);
        }
    
        int m_size;
    };
    
    QList<QImage> images = ...;
    QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100));

    使用绑定函数参数

    如果你想使用一个map函数,它接受多个参数可以使用std::bind()来将其转换到一个接受一个参数的函数上。如果c++ 11支持不可用,boost::bind()或std::tr1::bind()是合适的替代品。

    举个例子,我们将使用QImage::scaledToWidth():

    QImage QImage::scaledToWidth(int width, Qt::TransformationMode) const;

    scaledToWidth接收三个参数(包括“this”指针),不能直接与QtConcurrent::mapped() 一起使用,因为QtConcurrent::mapped()需要接受一个参数的函数。为了使用QImage::scaledToWidth() with QtConcurrent::mapped(),我们必须为width和transformation mode提供一个值:

    std::bind(&QImage::scaledToWidth, 100, Qt::SmoothTransformation)

    std::bind()的返回值是一个具有以下签名的函数对象(functor):

    QImage scaledToWith(const QImage &image)

    这符合QtConcurrent::mapped()期望的,完整的示例变为:

    QList<QImage> images = ...;
    QFuture<QImage> thumbnails = QtConcurrent::mapped(images, std::bind(&QImage::scaledToWidth, 100 Qt::SmoothTransformation));
  • 相关阅读:
    [iOS]C语言技术视频-15-指针变量高级用法练习一(函数指针完成动态排序)
    [iOS]C语言技术视频-14-指针变量高级用法(函数指针)
    [iOS]C语言技术视频-13-指针变量练习三(结构体指针变量)
    [iOS]C语言技术视频-12-指针变量练习二(数组打印)
    ReentrantLock加锁解锁过程
    spring初始化bean之后的3种回调方式
    spring循环引用-笔记
    二叉树的三种遍历方式
    快速排序
    java模拟实现单向链表
  • 原文地址:https://www.cnblogs.com/itrena/p/5938237.html
Copyright © 2011-2022 走看看