QtConcurrent Qt处理多线程

先学下单词,微笑

concurrent 并发

有道词典结果

n. [数] 共点;同时发生的事件

adj. 并发的;一致的;同时发生的


这个QtConcurrent的命名控件提供了可以用来实现程序多线程的高级api, 而不用使用低级的线程的原始的api, 诸如:mutexes, read-write locks, wait conditions, or semaphores.

程序可以通过QtConcurrent类可以根据处理器的内核数量来自动调节使用线程的数量。这就意味着今天编写的应用程序可以在部署在未来的多核的机器上。

它为了进行并行的列表处理包含了一些功能性编程风格的api, 包括 MapReduce, FilterReduce他们通过内存的共享(非分布式的)系统来实现的,还有一些GUI应用程序用来进行管理异步计算的类们。

Qt Concurrent 支持少量的几个 STL-compatible容器和迭代器类型, 但是它和Qt 的拥有可以随机访问迭代器的容易协作的非常好, 如QList, QVector, 这个map和filter函数接收包括容器和 begin/end iterators.

支持的STL迭代器有:

Iterator Type Example classes Support status
Input Iterator   Not Supported
Output Iterator   Not Supported
Forward Iterator std::slist Supported
Bidirectional Iterator QLinkedList, std::list Supported
Random Access Iterator QListQVector, std::vector Supported and Recommended
随机访问的迭代器可以运行的很快在Qt Concurrent正在迭代大量轻量级的items时, 因为他们可以允许在容器中跳过某些点。 另外, 使用随机迭代器访问可以允许Qt Concurrent来提供运行进行的信息通过    QFuture::progressValue () and  QFutureWatcher::progressValueChanged ().

这个不合时宜的修正一些函数如 mapped() 和 filtered() 复制了一个容器在调用的时候, 如果你正在使用STL容器来复制操作可能会消耗一些时间, 为了避免这种情况,我们建议指定容器开始和结束时使用的迭代器。

 以Qt自带的一个wordCount的例子来看并发的性能:

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

#include <qtconcurrentmap.h>

using namespace QtConcurrent;

/*
    Utility function that recursivily searches for files.
*/
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;

/*
    Single threaded word counter function.
*/
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(QString word, textStream.readLine().split(" "))
                wordCount[word] += 1;

    }
    return wordCount;
}


// countWords counts the words in a single file. This function is
// called in parallel by several threads and must be thread
// safe.
WordCount countWords(const QString &file)
{
    QFile f(file);
    f.open(QIODevice::ReadOnly);
    QTextStream textStream(&f);
    WordCount wordCount;

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

    return wordCount;
}

// reduce adds the results from map to the final
// result. This functor will only be called by one thread
// at a time.
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";

    qDebug() << "warmup";
    {
        QTime time;
        time.start();
        WordCount total = singleThreadedWordCount(files);
    }

    qDebug() << "warmup done";

    int singleThreadTime = 0;
    {
        QTime time;
        time.start();
        WordCount total = singleThreadedWordCount(files);
        singleThreadTime = time.elapsed();
        qDebug() << "single thread" << singleThreadTime;
    }

    int mapReduceTime = 0;
    {
        QTime time;
        time.start();
        WordCount total = mappedReduced(files, countWords, reduce);
        mapReduceTime = time.elapsed();
        qDebug() << "MapReduce" << mapReduceTime;
    }
    qDebug() << "MapReduce speedup x" << ((double)singleThreadTime - (double)mapReduceTime) / (double)mapReduceTime + 1;
}

附上终端的执行结果,多线程并行结果比单线程的执行快了(947-692)毫秒。。。,对于大型程序来讲是很有意义的。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值