PDA

View Full Version : qt thread question for different algorithm thread



leonardhead
19th August 2010, 05:26
Hi, guys,

I am new the the qt thread, most of the example I found online is about multithread for one algorithm.

I have a problem to multithread 4 different algorithm at the same time to speed things up.

for example I have 3 thread totally different Thread class,

class ThreadA: public QThread
{}
class ThreadB: public QThread
{}
class ThreadC :public QThread
{}


void main()
{
ThreadA a;
ThreadB b;
ThreadC c;

a.start();
b.start();
c.start();

}


my program is sorta like the above structure, however when I run it, it doesnt look like it was multithreading running .

I am just wondering if anyone ever try to run different algorithm at the same time using the qt thread.

thank you

hobbyist
19th August 2010, 08:45
however when I run it, it doesnt look like it was multithreading running

How do you arrive at this conclusion?

The structure of your code seems more or less correct. But before returning from main(), it might be a good idea to wait for all threads to finish (for example, add a.wait(), b.wait(), and c.wait() in the end of your main()). Moreover, it is always a good practice to instantiate either QCoreApplication (console app) or QApplication (gui app) in the beginning of main().

leonardhead
19th August 2010, 20:27
I put the wait() at the end for sure, and the cpu usage is 75% for the whole time, and it seems like a multithread.

but the memory usage doesnt increase and the running time is as same as I running the three algorithm in sequence.


I am just wondering if there something I did wrong

Talei
19th August 2010, 21:06
If You are on Windows try SysinternalsSuite, more precisely ProcessExplorer. It has tab Threads that shows how many threads are running at the present time etc.

leonardhead
20th August 2010, 01:16
I use the software, it shows actually 3 thread is running at the same time.

but the running time is too long. even longer than the sequence running

is the multithread supposed to speed up the program ?

Talei
20th August 2010, 04:10
Multithreading is used basically in one of the two conditions.
- to "unfreeze" GUI thread (main window, this don't speedup Your program, It only keep your GUI responsive IF YOU HAVE 1CORE CPU, on multi core speedup )
- to speed up calculations
Depending on the task speedup very from few % to around 95%. Good example of that is CPU intense tasks like 3D rendering (programs like 3D studio Max, Maya, XSI, LightWave etc..., for example 1Core CPU render around 90-95% slower then 2Core CPU).
Please remember that not all algorithms are parallel scalable, so that means not always gain is as significant as the one in 3D Rendering (like global illumination, ambient occlusion etc..)

Please also note that when You have 1Core CPU Your task are divide into (in Your case) 3 threads that are running ON THE SAME CPU/CORE so maybe that's why You don't see the difference. Basically nowadays Threads should be run on the Multi Core CPU (or to "unfreeze" GUI), that way task can be assigned to individual CORE so You program utilize 100% of the CPU power.
Also the way task is assigned to the specific CORE is controlled by Your Operating System. If you have 2Cores and 3 threads it's obvious that 1Core will process 2threads, so maybe that's why speedup is not so significant in Your case.

As of qt threads I use it, and many other people here, for example in task like parallel image thumbnails downloading (up to N threads, when N < image thumbnails count), and it works like a charm, so probably You do something wrong or Your program logic is wrong.
Regards

leonardhead
27th August 2010, 01:10
Multithreading is used basically in one of the two conditions.
- to "unfreeze" GUI thread (main window, this don't speedup Your program, It only keep your GUI responsive IF YOU HAVE 1CORE CPU, on multi core speedup )
- to speed up calculations
Depending on the task speedup very from few % to around 95%. Good example of that is CPU intense tasks like 3D rendering (programs like 3D studio Max, Maya, XSI, LightWave etc..., for example 1Core CPU render around 90-95% slower then 2Core CPU).
Please remember that not all algorithms are parallel scalable, so that means not always gain is as significant as the one in 3D Rendering (like global illumination, ambient occlusion etc..)

Please also note that when You have 1Core CPU Your task are divide into (in Your case) 3 threads that are running ON THE SAME CPU/CORE so maybe that's why You don't see the difference. Basically nowadays Threads should be run on the Multi Core CPU (or to "unfreeze" GUI), that way task can be assigned to individual CORE so You program utilize 100% of the CPU power.
Also the way task is assigned to the specific CORE is controlled by Your Operating System. If you have 2Cores and 3 threads it's obvious that 1Core will process 2threads, so maybe that's why speedup is not so significant in Your case.

As of qt threads I use it, and many other people here, for example in task like parallel image thumbnails downloading (up to N threads, when N < image thumbnails count), and it works like a charm, so probably You do something wrong or Your program logic is wrong.
Regards



well, thank you for your reply,


basically my algorithm is a big while() loop which loops for 3million times.

class A: QThread
{
void run()
{
while()
{}
}
}


and in the main funciton

main()
{
A a1;
A a2;

a1.start()
a2.start()

a1.wait()
a2.wait()
}


I run it on a four core cpu, but the time is still like I run them in sequece.