Results 1 to 6 of 6

Thread: Multi-threading

  1. #1
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    20
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Multi-threading

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multi-threading

    Quote Originally Posted by lixo1 View Post
    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: [wiki]Keeping the GUI Responsive[/wiki].
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    lixo1 (22nd June 2009)

  4. #3
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    20
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi-threading

    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.

  5. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multi-threading

    Hi,
    Quote Originally Posted by lixo1 View Post
    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

    Quote Originally Posted by lixo1 View Post
    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.

    Quote Originally Posted by lixo1 View Post
    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.

    Quote Originally Posted by lixo1 View Post
    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.
    Òscar Llarch i Galán

  6. The following user says thank you to ^NyAw^ for this useful post:

    lixo1 (22nd June 2009)

  7. #5
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    20
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi-threading

    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.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multi-threading

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Application 3-tiers (multi layers)
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2009, 17:28
  2. py2app-deployed PyQt app -- throws QObject threading error
    By tory108 in forum Installation and Deployment
    Replies: 4
    Last Post: 20th January 2009, 20:16
  3. QTcpServer - Multi thread or single?
    By December in forum Qt Programming
    Replies: 3
    Last Post: 8th February 2008, 10:31
  4. Multi threading ...
    By kiranraj in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2007, 16:51
  5. multi screen
    By Thomas Feldman in forum Qt Programming
    Replies: 5
    Last Post: 9th May 2007, 16:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.