Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why when I use QThread in Qt is slower from a simple Qt main program?
Forum Updated to NodeBB v4.3 + New Features

Why when I use QThread in Qt is slower from a simple Qt main program?

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 5.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on last edited by
    #2

    Hi @stackprogramer ,

    Can you please show us:

    1. How do you call RunHardAlgorithm() in main()?
    2. How do you run MyComputationThread?
    3. How do you measure your code's speed?

    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

    1 Reply Last reply
    2
    • stackprogramerS stackprogramer

      Hi, my friend I want to use QThread in my program:
      when I used main program, it is intermediate, when I used QThread for running algorithm.

      RunHardAlgorithm(){
      
      }
      
      
      class MyComputationThread :  public QThread
      {
          Q_OBJECT
          // some methods
          // some variables
         Run()
          { RunHardAlgorithm();
      
            
      
         
          }
      }
      

      i used setPriority too, but it is slow.........
      How i can assign multi core to a thread,please guide
      my means why main Thread is so faster than other QThread that we defined?????

      I saw this post
      http://stackoverflow.com/questions/11188080/how-to-force-using-multicore-in-qthread-with-openmp-function

      Best regards stackprogramer

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @stackprogramer "How i can assign multi core to a thread" - what do you mean? A thread is either running on one core or sleeping. It cannot run on several cores at the same time. That's why you need at least one thread for each core if you want to utilize all of them.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • stackprogramerS Offline
        stackprogramerS Offline
        stackprogramer
        wrote on last edited by stackprogramer
        #4

        @jsulm i have cpu corei5, i want to when main thread is busy, itshould be work(main thread is usded for receiving big data upd socket............),but som times it is not busy,

        when data is not received, i only used one core for processing my Qthread, it is slow,i can not use in more optimization way of my cpu cores.......
        i lost other core cpu in that time......................

        @JKSH

        1. i run RunHardAlgorithm() in a signal udp data received, after that data is buffered in class upd i run RunHardAlgorithm().....................

        2)MyComputationThread ,......
        i run in a timer ........
        3)i measure seconds..................

        thanks

        jsulmJ JKSHJ 2 Replies Last reply
        0
        • stackprogramerS stackprogramer

          @jsulm i have cpu corei5, i want to when main thread is busy, itshould be work(main thread is usded for receiving big data upd socket............),but som times it is not busy,

          when data is not received, i only used one core for processing my Qthread, it is slow,i can not use in more optimization way of my cpu cores.......
          i lost other core cpu in that time......................

          @JKSH

          1. i run RunHardAlgorithm() in a signal udp data received, after that data is buffered in class upd i run RunHardAlgorithm().....................

          2)MyComputationThread ,......
          i run in a timer ........
          3)i measure seconds..................

          thanks

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @stackprogramer Sorry I don't understand what you're writing. Sounds like you're doing multi-threading in a wrong way.
          You should read https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          J.HilkJ 1 Reply Last reply
          0
          • stackprogramerS Offline
            stackprogramerS Offline
            stackprogramer
            wrote on last edited by stackprogramer
            #6

            @jslum sorry i am in hurry
            i saw this links
            How To Really, Truly Use QThreads; The Full Explanation

            http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

            http://www.bogotobogo.com/Qt/Qt5_QThreads_Creating_Threads.php

            http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

            https://github.com/fabienpn/simple-qt-thread-example

            https://john.nachtimwald.com/2015/05/02/effective-threading-using-qt/

            but my problem has so complication algorithm, i need a effective way. any way thanks very much

            kshegunovK 1 Reply Last reply
            0
            • jsulmJ jsulm

              @stackprogramer Sorry I don't understand what you're writing. Sounds like you're doing multi-threading in a wrong way.
              You should read https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #7

              @jsulm

              I think the issue here is not with QThread

              @stackprogramer wants his program to use more than 1 CPU core for each thread to make the most out of the available hardware

              Mulitthread != Multicore


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              jsulmJ 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @jsulm

                I think the issue here is not with QThread

                @stackprogramer wants his program to use more than 1 CPU core for each thread to make the most out of the available hardware

                Mulitthread != Multicore

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @J.Hilk said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                use more than 1 CPU core for each thread

                This is not possible. A thread can only run on one CPU at given time. If you want to utilize 4 cores you need at least 4 threads.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • stackprogramerS Offline
                  stackprogramerS Offline
                  stackprogramer
                  wrote on last edited by stackprogramer
                  #9

                  @J-Hilk @jsulm thanks for attention
                  i think i should switch to multicorre like cuda gpu programming or a same tools.
                  thread is good but it is not Excellent for my problem.
                  any way thanks ,best regards stackprogramer

                  J.HilkJ 1 Reply Last reply
                  0
                  • stackprogramerS stackprogramer

                    @jsulm i have cpu corei5, i want to when main thread is busy, itshould be work(main thread is usded for receiving big data upd socket............),but som times it is not busy,

                    when data is not received, i only used one core for processing my Qthread, it is slow,i can not use in more optimization way of my cpu cores.......
                    i lost other core cpu in that time......................

                    @JKSH

                    1. i run RunHardAlgorithm() in a signal udp data received, after that data is buffered in class upd i run RunHardAlgorithm().....................

                    2)MyComputationThread ,......
                    i run in a timer ........
                    3)i measure seconds..................

                    thanks

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #10

                    @stackprogramer said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                    @JKSH

                    1. i run RunHardAlgorithm() in a signal udp data received, after that data is buffered in class upd i run RunHardAlgorithm().....................

                    2)MyComputationThread ,......
                    i run in a timer ........
                    3)i measure seconds..................

                    Sorry, your descriptions are unclear... please post your code.

                    @stackprogramer said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                    i think i should switch to multicorre like cuda gpu programming or a same tools.
                    thread is good but it is not Excellent for my problem.

                    Do you mean you want to do parallel computing? What is the name of your algorithm?

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    1
                    • stackprogramerS stackprogramer

                      @J-Hilk @jsulm thanks for attention
                      i think i should switch to multicorre like cuda gpu programming or a same tools.
                      thread is good but it is not Excellent for my problem.
                      any way thanks ,best regards stackprogramer

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

                      @stackprogramer

                      You can always break it up "manually"

                      QList<int> lInt;
                      
                      int cnt = QThread::idealThreadCount();
                      
                      if(cnt > 1){
                        int size = lInt.size()/cnt;
                         for(int i = 0; i < cnt; i++){
                            QList<int> newlInt = lInt.mid(size*i,size);
                            QtConcurrent::run(this, &MyClass::RunHardAlgorithm, newlInt);
                         }
                      }else{
                         QtConcurrent::run(this, &MyClass::RunHardAlgorithm, lInt);
                      }
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      1
                      • stackprogramerS stackprogramer

                        @jslum sorry i am in hurry
                        i saw this links
                        How To Really, Truly Use QThreads; The Full Explanation

                        http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

                        http://www.bogotobogo.com/Qt/Qt5_QThreads_Creating_Threads.php

                        http://blog.debao.me/2013/08/how-to-use-qthread-in-the-right-way-part-1/

                        https://github.com/fabienpn/simple-qt-thread-example

                        https://john.nachtimwald.com/2015/05/02/effective-threading-using-qt/

                        but my problem has so complication algorithm, i need a effective way. any way thanks very much

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #12

                        From what I was able to grasp from your explanation it seems to me you need to parallelize your algorithm rather. However this depends on what your calculation does, and making a sequential computation a parallel one is a big universe; there's no one approach to help with that, and there's no easy fix by just using a class or w/e. As @JKSH said, you need to post your code, so we can examine it and provide a constructive suggestion.

                        @J.Hilk said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                        Mulitthread != Multicore

                        For most cases that's not true. The only distinction between the two can be made if one considers the memory - if it's shared or distributed, and even then the argument is rather weak. @jsulm makes a good point, that a thread can be executed only on a single core and a core can execute a single thread at any one time.

                        Read and abide by the Qt Code of Conduct

                        J.HilkJ 1 Reply Last reply
                        1
                        • kshegunovK kshegunov

                          From what I was able to grasp from your explanation it seems to me you need to parallelize your algorithm rather. However this depends on what your calculation does, and making a sequential computation a parallel one is a big universe; there's no one approach to help with that, and there's no easy fix by just using a class or w/e. As @JKSH said, you need to post your code, so we can examine it and provide a constructive suggestion.

                          @J.Hilk said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                          Mulitthread != Multicore

                          For most cases that's not true. The only distinction between the two can be made if one considers the memory - if it's shared or distributed, and even then the argument is rather weak. @jsulm makes a good point, that a thread can be executed only on a single core and a core can execute a single thread at any one time.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #13

                          @kshegunov said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                          @J.Hilk said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                          Mulitthread != Multicore

                          For most cases that's not true. The only distinction between the two can be made if one considers the memory - if it's shared or distributed, and even then the argument is rather weak.

                          What I tried to say was, that one does not a necessarily implicate the other.

                          You can have multiple threads that are handled from a single core.
                          Also, creating a new thread, its also not automaticaly assured that its handled from a different core as well, I believe that is handled by the OS and the current CPU-load?


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          kshegunovK 1 Reply Last reply
                          0
                          • J.HilkJ J.Hilk

                            @kshegunov said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                            @J.Hilk said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                            Mulitthread != Multicore

                            For most cases that's not true. The only distinction between the two can be made if one considers the memory - if it's shared or distributed, and even then the argument is rather weak.

                            What I tried to say was, that one does not a necessarily implicate the other.

                            You can have multiple threads that are handled from a single core.
                            Also, creating a new thread, its also not automaticaly assured that its handled from a different core as well, I believe that is handled by the OS and the current CPU-load?

                            kshegunovK Offline
                            kshegunovK Offline
                            kshegunov
                            Moderators
                            wrote on last edited by kshegunov
                            #14

                            @J.Hilk said in Why when I use QThread in Qt is slower from a simple Qt main program?:

                            What I tried to say was, that one does not a necessarily implicate the other.

                            I'm arguing they're one and the same. :)

                            You can have multiple threads that are handled from a single core

                            Yes, by means of context switching.

                            Also, creating a new thread, its also not automaticaly assured that its handled from a different core as well, I believe that is handled by the OS and the current CPU-load?

                            What core executes what thread at a given time slice is handled by the OS, yes. The actual implementation is in the OS' scheduler.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            2

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved