Skip to content

Instantly share code, notes, and snippets.

@j2doll
Created January 19, 2018 06:45
Show Gist options
  • Select an option

  • Save j2doll/c40d0d5025afd8d65867e5def076387a to your computer and use it in GitHub Desktop.

Select an option

Save j2doll/c40d0d5025afd8d65867e5def076387a to your computer and use it in GitHub Desktop.
QtConcurrent : MapReduce
#include <climits>
#include <QtGlobal>
#include <QString>
#include <QDebug>
#include <QtCore>
#include <QtCore/QCoreApplication>
#include <QThread>
#include <QtConcurrentRun>
#include <QTime>
#include <QDateTime>
#ifndef QT_NO_CONCURRENT
using namespace QtConcurrent;
void timeConsumingFunction(QString name)
{
qDebug() << QThread::currentThread()
<< "(" << __FUNCTION__ << ")"
<< "[START]\t" << "NAME : " << name;
QTime spanTime;
spanTime.start();
double cardSoldier = 0;
for ( int heart = 1 ; heart < 20000 ; heart ++ )
{
for ( int spade = 1 ; spade < 20000 ; spade ++ )
{
cardSoldier = cardSoldier + ( double(heart) / double(spade) );
}
}
quint32 milliSecond = spanTime.elapsed();
qDebug() << QThread::currentThread()
<< "(" << __FUNCTION__ << ")"
<< "[END]\t" << "NAME : " << name
<< ", elapsed time = " << milliSecond ;
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
// the amount of cores on your pc
qDebug() << " IDLE THREAD COUNT : " << QThread::idealThreadCount ();
QFuture<void> helloAlice = run( timeConsumingFunction, QString("Alice") );
QFuture<void> helloWhiteRabbit = run( timeConsumingFunction, QString("White Rabbit") );
QFuture<void> helloMadHatter = run( timeConsumingFunction, QString("Mad Hatter") );
QFuture<void> helloRedQueen = run( timeConsumingFunction, QString("Red Queen") );
QFuture<void> helloHumptyDumpty = run( timeConsumingFunction, QString("Humpty Dumpty") );
QTime spanTime;
spanTime.start();
helloAlice.waitForFinished();
helloWhiteRabbit.waitForFinished();
helloMadHatter.waitForFinished();
helloRedQueen.waitForFinished();
helloHumptyDumpty.waitForFinished();
quint32 milliSecond = spanTime.elapsed();
qDebug() << QThread::currentThread()
<< "(" << __FUNCTION__ << ")"
<< " elapsed time = "<< milliSecond ;
return app.exec();
}
#else
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
qDebug() << "Qt Concurrent is not yet supported on this platform";
return 0;
}
#endif
//
// *** Concurrent Programming for Qt ***
//
// The QtConcurrent namespace provides high-level APIs that make it possible
// to write multi-threaded programs without using low-level threading primitives
// such as mutexes, read-write locks, wait conditions, or semaphores.
// Programs written with QtConcurrent automatically adjust the number of threads
// used according to the number of processor cores available. This means that
// applications written today will continue to scale when deployed on
// multi-core systems in the future.
//
// QtConcurrent includes functional programming style APIs for parallel list p
// rocessing, including a MapReduce and FilterReduce implementation for
// shared-memory (non-distributed) systems, and classes for managing
// asynchronous computations in GUI applications:
//
// * 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.
// * 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.
// * 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.
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment