先学下单词,:
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.
支持的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 |
这个不合时宜的修正一些函数如 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)毫秒。。。,对于大型程序来讲是很有意义的。