×
Namespaces

Variants
Actions
(Difference between revisions)

How to add multithread support to your Qt application

From Nokia Developer Wiki
Jump to: navigation, search
hugovk (Talk | contribs)
(Hugovk - - Introduction: Improved English)
hugovk (Talk | contribs)
(Hugovk - Improved English and formatting)
Line 1: Line 1:
 
[[Category:Qt]][[Category:Symbian]][[Category:MeeGo]][[Category:Code Examples]]
 
[[Category:Qt]][[Category:Symbian]][[Category:MeeGo]][[Category:Code Examples]]
 
 
{{Note|This is an entry in the [[PureView Imaging Competition 2012Q2]]}}
 
{{Note|This is an entry in the [[PureView Imaging Competition 2012Q2]]}}
  
Line 30: Line 29:
  
 
== Showcase  ==
 
== Showcase  ==
[http://projects.developer.nokia.com/QHdrCamera/ QHdrCamera] is a component component able to create [http://en.wikipedia.org/wiki/High_dynamic_range_imaging  High Dynamic Range images].<br />
+
[http://projects.developer.nokia.com/QHdrCamera/ QHdrCamera] is a component component able to create [http://en.wikipedia.org/wiki/High_dynamic_range_imaging  High Dynamic Range images].
Basically to do that the component captures three photos at different exposure range mixing them using some algorithms. For further information, please referr to [http://www.developer.nokia.com/Community/Wiki/QHdrCamera_component_for_High_Dynamic_Range_Imaging this article].<br />
+
 
<br />
+
Basically to do that the component captures three photos at different exposure range mixing them using some algorithms. For further information, please refer to [http://www.developer.nokia.com/Community/Wiki/QHdrCamera_component_for_High_Dynamic_Range_Imaging this article].
The capture image in sequence have to be faster than possible. Heach time the QML component will be asked to shot a frame it saves in a default path the captured image and after is ready to capture another frame.<br />
+
 
At the end of the three image capturing process, the flow process need to load all three photo into memory for the raw image processing to produce our HDR image.<br />
+
Capturing images in sequence has to be done as fast as possible. Each time the QML component will be asked to shoot a frame it saves the captured image in the default path, after which it is ready to capture another frame.
The loading process is very slow, expecially at resolution growing, due to the ( time of writing ) phone memory speed, so this has impacts the overall processing time. If during the next frame shooting application was able to begin the previous saved frame into memory this will reduce the waiting time, enhancing user experience.<br />
+
 
The solution provided uses [http://qt-project.org/doc/qt-4.8/qrunnable.html QRUnnable] interface and [http://qt-project.org/doc/qt-4.8/qthreadpool.html QThreadPool] class.
+
After the process of capturing three images, the flow process need to load all three photo into memory for the raw image processing to produce our HDR image.
 +
 
 +
The loading process is very slow, especially as resolution grows, due to the (at time of writing, May 2012) phone memory speed, so this impacts the overall processing time. If, during shooting the next frame, the application was also able to begin loading the previously saved frame into memory this will reduce the waiting time, enhancing user experience.
 +
 
 +
The solution provided uses [http://qt-project.org/doc/qt-4.8/qrunnable.html QRunnable] interface and [http://qt-project.org/doc/qt-4.8/qthreadpool.html QThreadPool] class.
  
 
== QRunnable ==
 
== QRunnable ==
The QRunnable class is an interface for representing a task or piece of code that needs to be executed, represented by your reimplementation of the run() function.<br />
+
The {{Icode|QRunnable}} class is an interface for representing a task or piece of code that needs to be executed, represented by your reimplementation of the {{Icode|run()}} function.
QThreadPool executes your code in a separate thread supporting execution of the same QRunnable more than once.
+
 
 +
{{Icode|QThreadPool}} executes your code in a separate thread supporting execution of the same {{Icode|QRunnable}} more than once.
  
 
== Multithread Image Loading ==
 
== Multithread Image Loading ==
The first step is to implement  the QRunnable interface:
+
The first step is to implement  the {{Icode|QRunnable}} interface:
 
<code cpp>
 
<code cpp>
 
class LoadingThread : public QRunnable
 
class LoadingThread : public QRunnable
Line 73: Line 77:
  
 
=== QThreadPool ===
 
=== QThreadPool ===
The QML Camera component provide the {{Icode|onImageSaved ( path )}} signal, called after the image has been written to the filesystem, in this context connected to {{Icode|setShot}} SLOTs. Here we create the LoadingThread to start in background the into memory loading process.
+
The QML Camera component provides the {{Icode|onImageSaved ( path )}} signal, called after the image has been written to the filesystem, in this context connected to {{Icode|setShot}} slots. Here we create the {{Icode|LoadingThread}} to start memory loading process in the background.
  
 
<code cpp>
 
<code cpp>
Line 107: Line 111:
 
</code>
 
</code>
  
Application have to wait until all threads finish their job. QThreadPool provide a method, called {{Icode|waitForDone()'}} to do that.
+
The application has to wait until all threads finish their jobs. {{Icode|QThreadPool}} provides a method called {{Icode|waitForDone()'}} that does that.
 +
 
 
<code cpp>
 
<code cpp>
 
QThreadPool::globalInstance()->waitForDone();
 
QThreadPool::globalInstance()->waitForDone();
 
</code>
 
</code>

Revision as of 18:41, 27 May 2012

Note.pngNote: This is an entry in the PureView Imaging Competition 2012Q2

Article Metadata
Tested with
Devices(s): Nokia C7-00, Nokia N8, Nokia N950
CompatibilityArticle
Keywords: Thread, QThread, QRunnable,QThreadPool
Created: galazzo (21 May 2012)
Last edited: hugovk (27 May 2012)

Contents

Introduction

This article explains how to manage multithreading in a mobile application. QHdrCamera will be used as a showcase.

Showcase

QHdrCamera is a component component able to create High Dynamic Range images.

Basically to do that the component captures three photos at different exposure range mixing them using some algorithms. For further information, please refer to this article.

Capturing images in sequence has to be done as fast as possible. Each time the QML component will be asked to shoot a frame it saves the captured image in the default path, after which it is ready to capture another frame.

After the process of capturing three images, the flow process need to load all three photo into memory for the raw image processing to produce our HDR image.

The loading process is very slow, especially as resolution grows, due to the (at time of writing, May 2012) phone memory speed, so this impacts the overall processing time. If, during shooting the next frame, the application was also able to begin loading the previously saved frame into memory this will reduce the waiting time, enhancing user experience.

The solution provided uses QRunnable interface and QThreadPool class.

QRunnable

The QRunnable class is an interface for representing a task or piece of code that needs to be executed, represented by your reimplementation of the run() function.

QThreadPool executes your code in a separate thread supporting execution of the same QRunnable more than once.

Multithread Image Loading

The first step is to implement the QRunnable interface:

class LoadingThread : public QRunnable
{
public:
LoadingThread(QString*, QImage*);
 
private:
QString* path;
QImage* image;
 
protected:
void run();
};
 
LoadingThread::LoadingThread(QString* path, QImage* image){
this->path = path;
this->image = image;
}
 
void LoadingThread::run() {
if( image->load(*path) ){
qDebug() << "Image loaded";
} else {
qDebug() << "Error during image loading " << *path;
}
}

QThreadPool

The QML Camera component provides the onImageSaved ( path ) signal, called after the image has been written to the filesystem, in this context connected to setShot slots. Here we create the LoadingThread to start memory loading process in the background.

void QHdrCamera::setShot1(QString path) {
if( !QFile::rename(path, QDir::toNativeSeparators(s_shot1)) ) {
qDebug() << "Impossible to rename shot1:" << path;
} else {
LoadingThread* tLoad = new LoadingThread(&s_shot1, &shot1);
QThreadPool::globalInstance()->start(tLoad);
emit savedShot1();
}
}
 
void QHdrCamera::setShot2(QString path) {
if( !QFile::rename(path, s_shot2) ) {
qDebug() << "Impossible to rename shot2";
} else {
LoadingThread* tLoad = new LoadingThread(&s_shot2, &shot2);
QThreadPool::globalInstance()->start(tLoad);
emit savedShot2();
}
}
 
void QHdrCamera::setShot3(QString path) {
if( !QFile::rename(path, s_shot3) ){
qDebug() << "Impossible to rename shot3";
} else {
LoadingThread* tLoad = new LoadingThread(&s_shot3, &shot3);
QThreadPool::globalInstance()->start(tLoad);
emit savedShot3();
}
}

The application has to wait until all threads finish their jobs. QThreadPool provides a method called waitForDone()' that does that.

QThreadPool::globalInstance()->waitForDone();
271 page views in the last 30 days.
×