diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:34:13 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:34:13 (GMT) |
commit | 67ad0519fd165acee4a4d2a94fa502e9e4847bd0 (patch) | |
tree | 1dbf50b3dff8d5ca7e9344733968c72704eb15ff /examples/qtconcurrent | |
download | Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.zip Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.tar.gz Qt-67ad0519fd165acee4a4d2a94fa502e9e4847bd0.tar.bz2 |
Long live Qt!
Diffstat (limited to 'examples/qtconcurrent')
-rw-r--r-- | examples/qtconcurrent/README | 38 | ||||
-rw-r--r-- | examples/qtconcurrent/imagescaling/imagescaling.cpp | 147 | ||||
-rw-r--r-- | examples/qtconcurrent/imagescaling/imagescaling.h | 82 | ||||
-rw-r--r-- | examples/qtconcurrent/imagescaling/imagescaling.pro | 13 | ||||
-rw-r--r-- | examples/qtconcurrent/imagescaling/main.cpp | 64 | ||||
-rw-r--r-- | examples/qtconcurrent/map/main.cpp | 82 | ||||
-rw-r--r-- | examples/qtconcurrent/map/map.pro | 14 | ||||
-rw-r--r-- | examples/qtconcurrent/progressdialog/main.cpp | 100 | ||||
-rw-r--r-- | examples/qtconcurrent/progressdialog/progressdialog.pro | 14 | ||||
-rw-r--r-- | examples/qtconcurrent/qtconcurrent.pro | 12 | ||||
-rw-r--r-- | examples/qtconcurrent/runfunction/main.cpp | 73 | ||||
-rw-r--r-- | examples/qtconcurrent/runfunction/runfunction.pro | 14 | ||||
-rw-r--r-- | examples/qtconcurrent/wordcount/main.cpp | 167 | ||||
-rw-r--r-- | examples/qtconcurrent/wordcount/wordcount.pro | 14 |
14 files changed, 834 insertions, 0 deletions
diff --git a/examples/qtconcurrent/README b/examples/qtconcurrent/README new file mode 100644 index 0000000..af659af --- /dev/null +++ b/examples/qtconcurrent/README @@ -0,0 +1,38 @@ +Qt 4 extends Qt's support for multithreaded applications with an API for +concurrent programming which includes implementations of the well-known +map-reduce and filter-reduce algorithms. + + +The example launcher provided with Qt can be used to explore each of the +examples in this directory. + +Documentation for these examples can be found via the Tutorial and Examples +link in the main Qt documentation. + + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/qtconcurrent/imagescaling/imagescaling.cpp b/examples/qtconcurrent/imagescaling/imagescaling.cpp new file mode 100644 index 0000000..d69adaa --- /dev/null +++ b/examples/qtconcurrent/imagescaling/imagescaling.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "imagescaling.h" +#include "math.h" + +#ifndef QT_NO_CONCURRENT + +const int imageSize = 100; + +QImage scale(const QString &imageFileName) +{ + QImage image(imageFileName); + return image.scaled(QSize(imageSize, imageSize), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +} + +Images::Images(QWidget *parent) +: QWidget(parent) +{ + setWindowTitle(tr("Image loading and scaling example")); + resize(800, 600); + + imageScaling = new QFutureWatcher<QImage>(this); + connect(imageScaling, SIGNAL(resultReadyAt(int)), SLOT(showImage(int))); + connect(imageScaling, SIGNAL(finished()), SLOT(finished())); + + openButton = new QPushButton(tr("Open Images")); + connect(openButton, SIGNAL(clicked()), SLOT(open())); + + cancelButton = new QPushButton(tr("Cancel")); + cancelButton->setEnabled(false); + connect(cancelButton, SIGNAL(clicked()), imageScaling, SLOT(cancel())); + + pauseButton = new QPushButton(tr("Pause/Resume")); + pauseButton->setEnabled(false); + connect(pauseButton, SIGNAL(clicked()), imageScaling, SLOT(togglePaused())); + + QHBoxLayout *buttonLayout = new QHBoxLayout(); + buttonLayout->addWidget(openButton); + buttonLayout->addWidget(cancelButton); + buttonLayout->addWidget(pauseButton); + buttonLayout->addStretch(); + + imagesLayout = new QGridLayout(); + + mainLayout = new QVBoxLayout(); + mainLayout->addLayout(buttonLayout); + mainLayout->addLayout(imagesLayout); + mainLayout->addStretch(); + setLayout(mainLayout); +} + +Images::~Images() +{ + imageScaling->cancel(); + imageScaling->waitForFinished(); +} + +void Images::open() +{ + // Cancel and wait if we are already loading images. + if (imageScaling->isRunning()) { + imageScaling->cancel(); + imageScaling->waitForFinished(); + } + + // Show a file open dialog at QDesktopServices::PicturesLocation. + QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Images"), + QDesktopServices::storageLocation(QDesktopServices::PicturesLocation), + "*.jpg *.png"); + + if (files.count() == 0) + return; + + // Do a simple layout. + qDeleteAll(labels); + labels.clear(); + + int dim = sqrt(qreal(files.count())) + 1; + for (int i = 0; i < dim; ++i) { + for (int j = 0; j < dim; ++j) { + QLabel *imageLabel = new QLabel; + imageLabel->setFixedSize(imageSize,imageSize); + imagesLayout->addWidget(imageLabel,i,j); + labels.append(imageLabel); + } + } + + // Use mapped to run the thread safe scale function on the files. + imageScaling->setFuture(QtConcurrent::mapped(files, scale)); + + openButton->setEnabled(false); + cancelButton->setEnabled(true); + pauseButton->setEnabled(true); +} + +void Images::showImage(int num) +{ + labels[num]->setPixmap(QPixmap::fromImage(imageScaling->resultAt(num))); +} + +void Images::finished() +{ + openButton->setEnabled(true); + cancelButton->setEnabled(false); + pauseButton->setEnabled(false); +} + +#endif // QT_NO_CONCURRENT + diff --git a/examples/qtconcurrent/imagescaling/imagescaling.h b/examples/qtconcurrent/imagescaling/imagescaling.h new file mode 100644 index 0000000..46dccf5 --- /dev/null +++ b/examples/qtconcurrent/imagescaling/imagescaling.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef IMAGESCALING_H +#define IMAGESCALING_H + +#include <QtGui> + +#ifndef QT_NO_CONCURRENT + +class Images : public QWidget +{ +Q_OBJECT +public: + Images(QWidget *parent = 0); + ~Images(); +public Q_SLOTS: + void open(); + void showImage(int num); + void finished(); +private: + QPushButton *openButton; + QPushButton *cancelButton; + QPushButton *pauseButton; + QVBoxLayout *mainLayout; + QList<QLabel *> labels; + QGridLayout *imagesLayout; + QFutureWatcher<QImage> *imageScaling; +}; + +#else + +// Dummy class required because QT_NO_CONCURRENT is not set when moc is run. +class Images : public QWidget +{ +Q_OBJECT +public Q_SLOTS: + void open() {} + void showImage(int) {} + void finished() {} +}; + +#endif // QT_NO_CONCURRENT + +#endif // IMAGESCALING_H diff --git a/examples/qtconcurrent/imagescaling/imagescaling.pro b/examples/qtconcurrent/imagescaling/imagescaling.pro new file mode 100644 index 0000000..fbf864a --- /dev/null +++ b/examples/qtconcurrent/imagescaling/imagescaling.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp imagescaling.cpp +HEADERS += imagescaling.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/imagescaling +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/imagescaling +INSTALLS += target sources diff --git a/examples/qtconcurrent/imagescaling/main.cpp b/examples/qtconcurrent/imagescaling/main.cpp new file mode 100644 index 0000000..c0bf9ce --- /dev/null +++ b/examples/qtconcurrent/imagescaling/main.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtGui> + +#ifndef QT_NO_CONCURRENT + +#include "imagescaling.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc,argv); + + Images imageView; + imageView.show(); + + return app.exec(); +} + +#else + +int main() +{ + qDebug() << "Qt Concurrent is not supported on this platform"; +} + +#endif diff --git a/examples/qtconcurrent/map/main.cpp b/examples/qtconcurrent/map/main.cpp new file mode 100644 index 0000000..35b484d --- /dev/null +++ b/examples/qtconcurrent/map/main.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QImage> +#include <QList> +#include <QThread> +#include <QDebug> +#include <QApplication> +#include <qtconcurrentmap.h> + +#ifndef QT_NO_CONCURRENT + +QImage scale(const QImage &image) +{ + qDebug() << "Scaling image in thread" << QThread::currentThread(); + return image.scaled(QSize(100, 100), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + const int imageCount = 20; + + // Create a list containing imageCount images. + QList<QImage> images; + for (int i = 0; i < imageCount; ++i) + images.append(QImage(1600, 1200, QImage::Format_ARGB32_Premultiplied)); + + // Use QtConcurrentBlocking::mapped to apply the scale function to all the + // images in the list. + QList<QImage> thumbnails = QtConcurrent::blockingMapped(images, scale); + + return 0; +} + +#else + +int main() +{ + qDebug() << "Qt Concurrent is not yet supported on this platform"; +} + +#endif diff --git a/examples/qtconcurrent/map/map.pro b/examples/qtconcurrent/map/map.pro new file mode 100644 index 0000000..f20267b --- /dev/null +++ b/examples/qtconcurrent/map/map.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +TARGET = mapdemo +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/map +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/map +INSTALLS += target sources diff --git a/examples/qtconcurrent/progressdialog/main.cpp b/examples/qtconcurrent/progressdialog/main.cpp new file mode 100644 index 0000000..249fbc1 --- /dev/null +++ b/examples/qtconcurrent/progressdialog/main.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#ifndef QT_NO_CONCURRENT + +using namespace QtConcurrent; + +const int iterations = 20; + +void spin(int &iteration) +{ + const int work = 1000 * 1000 * 40; + volatile int v = 0; + for (int j = 0; j < work; ++j) + ++v; + + qDebug() << "iteration" << iteration << "in thread" << QThread::currentThreadId(); +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + // Prepare the vector. + QVector<int> vector; + for (int i = 0; i < iterations; ++i) + vector.append(i); + + // Create a progress dialog. + QProgressDialog dialog; + dialog.setLabelText(QString("Progressing using %1 thread(s)...").arg(QThread::idealThreadCount())); + + // Create a QFutureWatcher and conncect signals and slots. + QFutureWatcher<void> futureWatcher; + QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset())); + QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel())); + QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int, int)), &dialog, SLOT(setRange(int, int))); + QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog, SLOT(setValue(int))); + + // Start the computation. + futureWatcher.setFuture(QtConcurrent::map(vector, spin)); + + // Display the dialog and start the event loop. + dialog.exec(); + + futureWatcher.waitForFinished(); + + // Query the future to check if was canceled. + qDebug() << "Canceled?" << futureWatcher.future().isCanceled(); +} + +#else + +int main() +{ + qDebug() << "Qt Concurrent is not yet supported on this platform"; +} + +#endif + diff --git a/examples/qtconcurrent/progressdialog/progressdialog.pro b/examples/qtconcurrent/progressdialog/progressdialog.pro new file mode 100644 index 0000000..12ba0bf --- /dev/null +++ b/examples/qtconcurrent/progressdialog/progressdialog.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/progressdialog +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/progressdialog +INSTALLS += target sources diff --git a/examples/qtconcurrent/qtconcurrent.pro b/examples/qtconcurrent/qtconcurrent.pro new file mode 100644 index 0000000..53008f0 --- /dev/null +++ b/examples/qtconcurrent/qtconcurrent.pro @@ -0,0 +1,12 @@ +TEMPLATE = subdirs +SUBDIRS = imagescaling \ + map \ + progressdialog \ + runfunction \ + wordcount + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS qtconcurrent.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent +INSTALLS += target sources diff --git a/examples/qtconcurrent/runfunction/main.cpp b/examples/qtconcurrent/runfunction/main.cpp new file mode 100644 index 0000000..c758f83 --- /dev/null +++ b/examples/qtconcurrent/runfunction/main.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QDebug> +#include <QThread> +#include <QString> +#include <qtconcurrentrun.h> +#include <QApplication> + +#ifndef QT_NO_CONCURRENT + +using namespace QtConcurrent; + +void hello(QString name) +{ + qDebug() << "Hello" << name << "from" << QThread::currentThread(); +} + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + QFuture<void> f1 = run(hello, QString("Alice")); + QFuture<void> f2 = run(hello, QString("Bob")); + f1.waitForFinished(); + f2.waitForFinished(); +} + +#else + +int main() +{ + qDebug() << "Qt Concurrent is not yet supported on this platform"; +} + +#endif diff --git a/examples/qtconcurrent/runfunction/runfunction.pro b/examples/qtconcurrent/runfunction/runfunction.pro new file mode 100644 index 0000000..9034476 --- /dev/null +++ b/examples/qtconcurrent/runfunction/runfunction.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/runfunction +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/runfunction +INSTALLS += target sources diff --git a/examples/qtconcurrent/wordcount/main.cpp b/examples/qtconcurrent/wordcount/main.cpp new file mode 100644 index 0000000..5008eaf --- /dev/null +++ b/examples/qtconcurrent/wordcount/main.cpp @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#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; +} + +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; +} + +#else + +int main() +{ + qDebug() << "Qt Concurrent is not yet supported on this platform"; +} + +#endif diff --git a/examples/qtconcurrent/wordcount/wordcount.pro b/examples/qtconcurrent/wordcount/wordcount.pro new file mode 100644 index 0000000..fba95ee --- /dev/null +++ b/examples/qtconcurrent/wordcount/wordcount.pro @@ -0,0 +1,14 @@ +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/wordcount +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtconcurrent/wordcount +INSTALLS += target sources |