Qt之Concurrent框架

简述

QtConcurrent 命名空间提供了高级 API,使得可以在不使用低级线程原语(例如:互斥、读写锁、等待条件或信号量)的情况下编写多线程程序,使用 QtConcurrent 编写的程序根据可用的处理器核心数自动调整所使用的线程数。这意味着,当在未来部署多核系统时,现在编写的应用程序将继续适应。

用法

在 C++ API changes 有关于 Qt Concurrent 的更改说明:

Qt Concurrent has been moved from Qt Core to its own module

意思是说,Qt Concurrent 已经被从 Qt Core 中移到自己的模块中了。所以,要链接到 Qt Concurrent 模块,需要在 qmake 项目文件中添加:

QT += concurrent
  • 1

注意: QtConcurrent::Exception 类被重命名为 QException,并且 QtConcurrent::UnhandledException 类被重命名为 QUnhandledException,他们仍然位于 Qt Core 中。

Qt Concurrent

QtConcurrent 包含了函数式编程风格 APIs 用于并行列表处理,包括用于共享内存(非分布式)系统的 MapReduce 和 FilterReduce 实现,以及用于管理 GUI 应用程序异步计算的类:

  • Concurrent Map 和 Map-Reduce

    • QtConcurrent::map():将一个函数应用于一个容器中的每一项,就地修改 items。
    • QtConcurrent::mapped():和 map() 类似,只是它返回一个包含修改内容的新容器。
    • QtConcurrent::mappedReduced():和 mapped() 类似,只是修改后的结果减少或组合成一个单一的结果。
  • Concurrent Filter 和 Filter-Reduce

    • QtConcurrent::filter():从一个容器中删除所有 items,基于一个 filter 函数的结果。
    • QtConcurrent::filtered():和 filter() 类似,只是它返回一个包含过滤内容的新容器。
    • QtConcurrent::filteredReduced():和 filtered() 类似,只是过滤后的结果减少或组合成一个单一的结果。
  • Concurrent Run

    • QtConcurrent::run():在另一个线程中运行一个函数。
  • QFuture:表示异步计算的结果

  • QFutureIterator:允许通过 QFuture 遍历可用的结果
  • QFutureWatcher:允许使用信号槽来监控一个 QFuture
  • QFutureSynchronizer:是一个方便的类,用于一些 QFutures 的自动同步

Qt Concurrent 支持多种兼容 STL 的容器和迭代器类型,但是最好使用具有随机访问迭代器的 Qt 容器,例如:QList 或 QVector。map 和 filter 函数都接受容器和 begin/end 迭代器。

STL 迭代器支持概述:

迭代器类型 示例类 支持状态
Input Iterator   不支持
Output Iterator   不支持
Forward Iterator std::slist 支持
Bidirectional Iterator QLinkedList, std::list 支持
Random Access Iterator QList, QVector, std::vector 支持和推荐

在 Qt Concurrent 迭代大量轻量级 items 的情况下,随机访问迭代器可以更快,因为它们允许跳过容器中的任何点。此外,使用随机访问迭代器允许 Qt Concurrent 通过 QFuture::progressValue() 和 QFutureWatcher::progressValueChanged() 来提供进度信息。

非就地修改的函数(例如:mapped() 和 filtered()),在调用时会创建容器的副本。如果正在使用的是 STL 容器,此复制操作可能需要一段时间,在这种情况下,建议为容器指定 begin 和 end 迭代器。

单词统计

厉害了 Concurrent,来看一个单词统计的示例:

#include <QList>
#include <QMap>
#include <QTextStream>
#include <QString>
#include <QStringList>
#include <QDir>
#include <QTime>
#include <QApplication>
#include <QDebug>

#include <qtconcurrentmap.h>

using namespace QtConcurrent;

// 递归搜索文件
QStringList findFiles(const QString &startDir, QStringList filters)
{
    QStringList names;
    QDir dir(startDir);

    foreach (QString file, dir.entryList(filters, QDir::Files))
        names += startDir + '/' + file;

    foreach (QString subdir, dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot))
        names += findFiles(startDir + '/' + subdir, filters);
    return names;
}

typedef QMap<QString, int> WordCount;

// 单线程单词计数器函数
WordCount singleThreadedWordCount(QStringList files)
{
    WordCount wordCount;
    foreach (QString file, files) {
        QFile f(file);
        f.open(QIODevice::ReadOnly);
        QTextStream textStream(&f);
        while (textStream.atEnd() == false)
            foreach (const QString &word, textStream.readLine().split(' '))
                wordCount[word] += 1;

    }
    return wordCount;
}

// countWords 计算单个文件的单词数,该函数由多个线程并行调用,并且必须是线程安全的。
WordCount countWords(const QString &file)
{
    QFile f(file);
    f.open(QIODevice::ReadOnly);
    QTextStream textStream(&f);
    WordCount wordCount;

    while (textStream.atEnd() == false)
        foreach (const QString &word, textStream.readLine().split(' '))
            wordCount[word] += 1;

    return wordCount;
}

// reduce 将 map 的结果添加到最终结果,该函数只能由一个线程一次调用。
void reduce(WordCount &result, const WordCount &w)
{
    QMapIterator<QString, int> i(w);
    while (i.hasNext()) {
        i.next();
        result[i.key()] += i.value();
    }
}

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    qDebug() << "finding files...";
    QStringList files = findFiles("../../", QStringList() << "*.cpp" << "*.h");
    qDebug() << files.count() << "files";

    int singleThreadTime = 0;
    {
        QTime time;
        time.start();
        // 单线程统计,与 mapreduce 机制实现的作对比
        WordCount total = singleThreadedWordCount(files);
        singleThreadTime = time.elapsed();
        // 打印出所耗费的时间
        qDebug() << "single thread" << singleThreadTime;
    }

    int mapReduceTime = 0;
    {
        QTime time;
        time.start();
        // mappedReduced 方式进行统计
        WordCount total = mappedReduced(files, countWords, reduce);
        mapReduceTime = time.elapsed();
        qDebug() << "MapReduce" << mapReduceTime;
    }
    // 输出 mappedReduced 方式比单线程处理方式要快的倍数
    qDebug() << "MapReduce speedup x" << ((double)singleThreadTime - (double)mapReduceTime) / (double)mapReduceTime + 1;
}
  • 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
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101

输出如下:

finding files… 
2185 files 
single thread 5221 
MapReduce 2256 
MapReduce speedup x 2.31427

可以看出,共查找了 2185 个文件,单线程使用了 5221 毫秒,MapReduce 方式使用了 2256 毫秒,比单线程要快 2.31427 倍。经过反复尝试,基本都在 2 倍以上。

更多参考

猜你喜欢

转载自blog.csdn.net/hu12306/article/details/79760589