QtConcurrent Qt处理多线程

news/2024/7/8 11:43:58

先学下单词,微笑

concurrent 并发

有道词典结果

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

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


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

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

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

  • Concurrent Map and Map-Reduce
    • QtConcurrent::map() applies a function to every item in a container, modifying the items in-place.
    • QtConcurrent::mapped() is like map(), except that it returns a new container with the modifications.
    • QtConcurrent::mappedReduced() is like mapped(), except that the modified results are reduced or folded into a single result.
  • Concurrent Filter and Filter-Reduce
    • QtConcurrent::filter() removes all items from a container based on the result of a filter function.
    • QtConcurrent::filtered() is like filter(), except that it returns a new container with the filtered results.
    • QtConcurrent::filteredReduced() is like filtered(), except that the filtered results are reduced or folded into a single result.
  • Concurrent Run
    • QtConcurrent::run() runs a function in another thread.
  • QFuture represents the result of an asynchronous computation.
  • QFutureIterator allows iterating through results available via QFuture.
  • QFutureWatcher allows monitoring a QFuture using signals-and-slots.
  • QFutureSynchronizer is a convenience class that automatically synchronizes several QFutures.
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 QList, QVector, 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)毫秒。。。,对于大型程序来讲是很有意义的。




http://www.niftyadmin.cn/n/3280954.html

相关文章

(1)线程的常用方法 (2)线程的同步机制 (3)网络编程的常识

1.线程的常用方法 static void sleep(long millis) - 用于使得当前正在执行的线程进入休眠状态&#xff0c;休眠参数指定的毫秒(重点)。 static void sleep(long millis, int nanos) - 用于休眠参数指定的毫秒纳秒&#xff0c;1秒1000毫秒 1毫秒1000微秒 1微秒1000纳秒 int g…

QFuture 类 Qt 控制线程

QFuture类能够获取一个一步计算的结果。 通过使用这个 Qt Concurrent 框架内的一个api来开始一次计算。 它可以用允许多个线程同步一个或多个结果在稍后的一段时间内计算完成。这个结果可以是任意一种类型&#xff0c;这个类型有默认的构造函数和拷贝构造函数。如果通过调用…

QFutureWatcher

QFutureWatcher 可以用来监测 QFuture 正在使用的信号和槽. QFutureWatcher 提供了关于 QFuture的信息和通知. 通过使用setFuture() 就可以开始监测 QFuture. future() 将会返回 future. 为了方便使用, 一些 QFuture 的函数也还是可以访问的。 QFutureWatcher: progressVal…

Java并发(零)教程目录

上网看博客的时候无意中发现了有一个Java并发的教程还不错&#xff0c;有20多篇讲并发的&#xff0c;一天翻译1篇似乎也不太难。Lets go!原文地址&#xff1a;http://tutorials.jenkov.com/java-concurrency/index.html下面是目录&#xff1a;Java Concurrency / Multithreadin…

记一次计算机网络作业

Telnet远程登录计算机网络实验 实验设备和环境&#xff1a; Win10 64位PC 安装了Wireshark软件和VMware软件&#xff0c;VMware中有Red Hat Linux虚拟机。相当于有两台主机&#xff0c;为了区分两台主机下文中称Windows宿主机为Windows&#xff0c;称虚拟机Red Hat为Linux。 实…

多线程 pthread学习之一

一个简单的多线程例子 thread_demo1.c : <pre name"code" class"cpp">#include <stdio.h> #include <stdlib.h> #include <time.h> #include <pthread.h>static void wait(void) {time_t start_time time(NULL);while (ti…

数据结构-排序-快排

快速排序 首先快速排序步骤&#xff1a; 首先选择轴值把待排序内容分为两部分&#xff0c;左边为小于或者等于轴值&#xff0c;右边为大于轴值然后对左右重复上面步骤直到整个序列有序直接上代码这里先写一次划分的代码这里的一次划分是那第一个数字为轴值&#xff0c;我们也可…

图像处理的基础学习

矢量图像从结构理论上来说是没有锯齿的&#xff0c;但是由于显示器的物理特性是点阵显示&#xff0c;所以矢量图像最终表现在屏幕上的时候也会有锯齿现象。也就是说矢量图象真正的优势并不是体现在最终表现效果上&#xff0c;而是在图像制作过程中可以不损失质量地进行各种拉伸…