From 38be0d13830efd2d98281c645c3a60afe05ffece Mon Sep 17 00:00:00 2001 From: Qt by Nokia Date: Wed, 27 Apr 2011 12:05:43 +0200 Subject: Initial import from the monolithic Qt. This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12 --- examples/qtconcurrent/README | 38 +++++ .../qtconcurrent/imagescaling/imagescaling.cpp | 146 ++++++++++++++++++ examples/qtconcurrent/imagescaling/imagescaling.h | 81 ++++++++++ .../qtconcurrent/imagescaling/imagescaling.pro | 17 +++ examples/qtconcurrent/imagescaling/main.cpp | 63 ++++++++ examples/qtconcurrent/map/main.cpp | 81 ++++++++++ examples/qtconcurrent/map/map.pro | 16 ++ examples/qtconcurrent/progressdialog/main.cpp | 99 ++++++++++++ .../qtconcurrent/progressdialog/progressdialog.pro | 16 ++ examples/qtconcurrent/qtconcurrent.pro | 17 +++ examples/qtconcurrent/runfunction/main.cpp | 72 +++++++++ examples/qtconcurrent/runfunction/runfunction.pro | 16 ++ examples/qtconcurrent/wordcount/main.cpp | 166 +++++++++++++++++++++ examples/qtconcurrent/wordcount/wordcount.pro | 16 ++ 14 files changed, 844 insertions(+) create mode 100644 examples/qtconcurrent/README create mode 100644 examples/qtconcurrent/imagescaling/imagescaling.cpp create mode 100644 examples/qtconcurrent/imagescaling/imagescaling.h create mode 100644 examples/qtconcurrent/imagescaling/imagescaling.pro create mode 100644 examples/qtconcurrent/imagescaling/main.cpp create mode 100644 examples/qtconcurrent/map/main.cpp create mode 100644 examples/qtconcurrent/map/map.pro create mode 100644 examples/qtconcurrent/progressdialog/main.cpp create mode 100644 examples/qtconcurrent/progressdialog/progressdialog.pro create mode 100644 examples/qtconcurrent/qtconcurrent.pro create mode 100644 examples/qtconcurrent/runfunction/main.cpp create mode 100644 examples/qtconcurrent/runfunction/runfunction.pro create mode 100644 examples/qtconcurrent/wordcount/main.cpp create mode 100644 examples/qtconcurrent/wordcount/wordcount.pro (limited to 'examples/qtconcurrent') diff --git a/examples/qtconcurrent/README b/examples/qtconcurrent/README new file mode 100644 index 0000000000..af659af2cc --- /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 0000000000..6ed7ae7511 --- /dev/null +++ b/examples/qtconcurrent/imagescaling/imagescaling.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $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(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 0000000000..096e880b1b --- /dev/null +++ b/examples/qtconcurrent/imagescaling/imagescaling.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef IMAGESCALING_H +#define IMAGESCALING_H + +#include + +#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 labels; + QGridLayout *imagesLayout; + QFutureWatcher *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 0000000000..b9601ad48b --- /dev/null +++ b/examples/qtconcurrent/imagescaling/imagescaling.pro @@ -0,0 +1,17 @@ +TEMPLATE = app +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp imagescaling.cpp +HEADERS += imagescaling.h + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/imagescaling +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/imagescaling +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) + +wince*: DEPLOYMENT_PLUGIN += qgif qjpeg qtiff diff --git a/examples/qtconcurrent/imagescaling/main.cpp b/examples/qtconcurrent/imagescaling/main.cpp new file mode 100644 index 0000000000..de64116f1c --- /dev/null +++ b/examples/qtconcurrent/imagescaling/main.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include + +#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 0000000000..bb6c5c657a --- /dev/null +++ b/examples/qtconcurrent/map/main.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#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 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 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 0000000000..76a19964e0 --- /dev/null +++ b/examples/qtconcurrent/map/map.pro @@ -0,0 +1,16 @@ +TEMPLATE = app +TARGET = mapdemo +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/map +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/map +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/qtconcurrent/progressdialog/main.cpp b/examples/qtconcurrent/progressdialog/main.cpp new file mode 100644 index 0000000000..307baed45b --- /dev/null +++ b/examples/qtconcurrent/progressdialog/main.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#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 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 connect signals and slots. + QFutureWatcher 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 0000000000..9ec11a4ae1 --- /dev/null +++ b/examples/qtconcurrent/progressdialog/progressdialog.pro @@ -0,0 +1,16 @@ +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/progressdialog +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/progressdialog +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/qtconcurrent/qtconcurrent.pro b/examples/qtconcurrent/qtconcurrent.pro new file mode 100644 index 0000000000..72af020e38 --- /dev/null +++ b/examples/qtconcurrent/qtconcurrent.pro @@ -0,0 +1,17 @@ +TEMPLATE = subdirs +SUBDIRS = imagescaling \ + map \ + runfunction \ + wordcount + +!wince* { + SUBDIRS += progressdialog +} + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS qtconcurrent.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/qtconcurrent/runfunction/main.cpp b/examples/qtconcurrent/runfunction/main.cpp new file mode 100644 index 0000000000..1e448bb71d --- /dev/null +++ b/examples/qtconcurrent/runfunction/main.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include + +#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 f1 = run(hello, QString("Alice")); + QFuture 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 0000000000..a003d9afe2 --- /dev/null +++ b/examples/qtconcurrent/runfunction/runfunction.pro @@ -0,0 +1,16 @@ +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/runfunction +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/runfunction +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) diff --git a/examples/qtconcurrent/wordcount/main.cpp b/examples/qtconcurrent/wordcount/main.cpp new file mode 100644 index 0000000000..035207c410 --- /dev/null +++ b/examples/qtconcurrent/wordcount/main.cpp @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#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 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 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 0000000000..40ae58891f --- /dev/null +++ b/examples/qtconcurrent/wordcount/wordcount.pro @@ -0,0 +1,16 @@ +TEMPLATE = app +TARGET += +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += main.cpp +CONFIG += console + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/wordcount +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/qtconcurrent/wordcount +INSTALLS += target sources + +symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) -- cgit v1.2.3