PDA

View Full Version : Multi-threading



lixo1
22nd June 2009, 10:08
Hi everybody,

I would like to verify, if my "vision" about Qt threads is correct:

1) To create a new thread, inside a Qt GUI application we need to use QThread class, that will create a separated thread that will uses all the CPU avaible (only 1 CPU).
If I need more power, using for example a PC with 2 or more CPUs, I need to use QtConcurrent that will use all the cpus avaibles. Is it correct?

2) I have a huge amount of calculations, the correct logic is to create a QThread and then inside the run() function use QtConcurrent (or for example OpenMP). Is it correct, or is there a more correct solution?

Thank you very much for any kind of explanation.

wysota
22nd June 2009, 10:17
1) To create a new thread, inside a Qt GUI application we need to use QThread class, that will create a separated thread that will uses all the CPU avaible (only 1 CPU).
If I need more power, using for example a PC with 2 or more CPUs, I need to use QtConcurrent that will use all the cpus avaibles. Is it correct?
Not really. Each thread will use at most one CPU, regardless if you use Qt Concurrent or not. It is only that Qt Concurrent usually spawns more than one thread thus using more than one CPU. But you can obtain the same effect if you start two QThread based threads yourself.


2) I have a huge amount of calculations, the correct logic is to create a QThread and then inside the run() function use QtConcurrent (or for example OpenMP). Is it correct, or is there a more correct solution?
No, if you want to use QtConcurrent, you don't have to create an external thread for that.

Read this article, it might be helpful: Keeping the GUI Responsive.

lixo1
22nd June 2009, 10:52
Thank you very much for all explanations, but I really don't understand that:

I has a PC with 2 CPUs, I created a QThread, it uses 100% of only 1 cpu.
Now you said that if I create another QThread, let say inside my first QThread, they will use the 2 CPUs?
What happens with my application when I run it in a QuadCore, it will use only 2?
Last question if I create more than 2 QThreads on my PC (2 CPU), the calculation will be divided into 3 or more parts, and the perfomance, how will be the more fast?

Thank you another time.

^NyAw^
22nd June 2009, 11:08
Hi,


I has a PC with 2 CPUs, I created a QThread, it uses 100% of only 1 cpu.

One Thread can only be executed by one CPU



Now you said that if I create another QThread, let say inside my first QThread, they will use the 2 CPUs?

You can create many threads. Then the OS will assign them into the proper CPU. So, starting 2 threads surely will use 2 CPUs.



What happens with my application when I run it in a QuadCore, it will use only 2?

The same that I told you. The OS is not able to divide your program to be executed into different CPUs. You have to divide your heavy process data into different threads to let them be executed on different CPUs.



Last question if I create more than 2 QThreads on my PC (2 CPU), the calculation will be divided into 3 or more parts, and the perfomance, how will be the more fast?

As I said, the OS will NOT divide your tasks!

If you create a simple console application that process data sequentially and it finish, you can have a Quad core or 1024 cores, the execution will be done in only one CPU. So if you want to use more CPUs you have to divide your task.

lixo1
22nd June 2009, 12:24
Hi,

Thank you, now I undestand. If I want to create an application that will use all CPUs avaibles inside the machine for different machines, the best way is to use QtConcurrent (or other lib that divided the jobs).

Thank you another time.

wysota
22nd June 2009, 14:22
Take into consideration that the CPUs are shared not among the threads of your application but all threads of all applications running in the system. So if you have a 4 core system, it doesn't mean that if you spawn 4 threads, the calculations will finish earlier than if you used 3 threads. It might be the other way round if there are other applications running in the system (especially if they rely on the processor heavily). Multithreading can be used most efficiently when there are two kinds of operations interfacing - calculations and I/O. When one thread is doing I/O operations it can give the CPU away to another thread.