文档详情

QT4.6的并行处理机制.pdf

发布:2017-06-02约字共3页下载文档
文本预览下载声明
QT4.6 的并行处理机制 google 的数据后端处理大家可能听说过,所使用的并行机制 是map-reduce, 也就是用map 去 收集整理一个数据集,之后用reduce 对数据集进行处理,得出想要的结果。原理很简单,但 是就是这个简单的原理构成了 google 搜索 的强大的基石,另外 google 的map-reduece 是分 布式的,呵呵 这段时间研究 qt4.6 ,它对于并行处理这一块做了非常不错的封装,使用很简单,不用考虑 线程同步处理等问题,下面就用这个最著名的统计单词个数的例子说 明,代码真实太简单 了 #include QList #include QMap #include QTextStream #include QString #include QStringList #include QDir #include QTime #include QApplication #include QDebug #include qtconcurrentmap.h #ifndef QT_NO_CONCURRENT 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; } typede QMapQString, int WordCount; /* Single threaded word counter function. */ WordCount singleThreadedWordCount (QStringList files) { WordCount wordCount; foreach (QString file, files) { QFile (file); f.open (QIODevice::ReadOnly); QTextStream textStream ( ); 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 (file); f.open (QIODevice::ReadOnly); QTextStream textStream ( ); WordCount wordCount; while (textStream.atEnd () == false) foreach (QString word, textStream.readLine ().split( )) wordCount [word] += 1; return wordCount; } // r
显示全部
相似文档