Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
QtConcurrent-simple-usage: Difference between revisions
Jump to navigation
Jump to search
AutoSpider (talk | contribs) (Decode HTML entity names) |
No edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
[[Category:HowTo]] | |||
[[Category: | |||
This code focuses on the following problem: | This code focuses on the following problem: | ||
We have a '''QList<Params>''' | We have a '''QList<Params>''' | ||
Line 10: | Line 5: | ||
The single core solution: | The single core solution: | ||
<code> | <code> | ||
void Optimizer::execute() | void Optimizer::execute() | ||
Line 15: | Line 11: | ||
QList<Params> params = generateParamsSet(); | QList<Params> params = generateParamsSet(); | ||
QList<Result> results; | QList<Result> results; | ||
for( int i = 0; i < params.size(); +''i ) { | for(int i = 0; i < params.size(); +''i) { | ||
results.append( someReallyTimeConsumingFunction(params) ); | |||
} | } | ||
//... | |||
</code> | </code> | ||
Let's use QtConcurrent to scale easily over the cores | Let's use QtConcurrent to scale easily over the cores: | ||
<code> | <code> | ||
Line 28: | Line 24: | ||
QList<Params> params = generateParamsSet(); | QList<Params> params = generateParamsSet(); | ||
QList<Result> results = QtConcurrent::blockingMapped( params, &Optimizer::calculateStats ); | QList<Result> results = QtConcurrent::blockingMapped( params, &Optimizer::calculateStats ); | ||
//... | |||
} | } | ||
Line 40: | Line 36: | ||
To make sure that our code always compiles, even when QtConcurrent is not supported, we may write something like this: | To make sure that our code always compiles, even when QtConcurrent is not supported, we may write something like this: | ||
<code> | <code> | ||
void Optimizer::execute() | void Optimizer::execute() | ||
Line 45: | Line 42: | ||
QList<Params> params = generateParamsSet(); | QList<Params> params = generateParamsSet(); | ||
QList<Result> results; | QList<Result> results; | ||
#ifndef QT_NO_CONCURRENT | |||
results = QtConcurrent::blockingMapped( params, &Optimizer::calculateStats ); | results = QtConcurrent::blockingMapped( params, &Optimizer::calculateStats ); | ||
#else | |||
for( int i = 0; i < params.size();''+i ) { | for(int i = 0; i < params.size();''+i) { | ||
results.append( someReallyTimeConsumingFunction(params) ); | |||
} | } | ||
#endif | |||
//... | |||
} | } | ||
</code> |
Latest revision as of 21:48, 7 April 2016
This code focuses on the following problem: We have a QList<Params> We need to run some time consuming function for each Params set on the list.
The single core solution:
void Optimizer::execute()
{
QList<Params> params = generateParamsSet();
QList<Result> results;
for(int i = 0; i < params.size(); +''i) {
results.append( someReallyTimeConsumingFunction(params) );
}
//...
Let's use QtConcurrent to scale easily over the cores:
void Optimizer::execute()
{
QList<Params> params = generateParamsSet();
QList<Result> results = QtConcurrent::blockingMapped( params, &Optimizer::calculateStats );
//...
}
Result Optimizer::calculateStats( const Params ¶ms )
{
return someReallyTimeConsumingFunction( params ); //this function returns the Result object.
}
Using it that way, we are sure that none of the cores is bored during our processing!
To make sure that our code always compiles, even when QtConcurrent is not supported, we may write something like this:
void Optimizer::execute()
{
QList<Params> params = generateParamsSet();
QList<Result> results;
#ifndef QT_NO_CONCURRENT
results = QtConcurrent::blockingMapped( params, &Optimizer::calculateStats );
#else
for(int i = 0; i < params.size();''+i) {
results.append( someReallyTimeConsumingFunction(params) );
}
#endif
//...
}