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. can we define a std::thread on QThread
Forum Updated to NodeBB v4.3 + New Features

can we define a std::thread on QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 648 Views 2 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.
  • stackprogramerS Online
    stackprogramerS Online
    stackprogramer
    wrote on last edited by
    #1

    Hi, my experience in using QThread, I see that QThread vs std::thread is slower, I have a question about how to use them. For managing signal/slots and GUI connection I need QThread, but it is slow for heavy computing use....is It possible to define a std::thread on QThread with a while flag...I want to manage of standard thread with QThread.... (starting and stopping)
    Can anyone guide or help, thanks in advance

    #include <iostream>
    #include <thread>
    
    class A {
    public:
        void print(bool& flag) {
            while (flag) {
                std::cout << "Printing..." << std::endl;
                // You can add more code logic here
                // ...
            }
        }
    };
    
    int main() {
        bool flag = true;
    
        A obj;
        std::thread threadObj(&A::print, &obj, std::ref(flag));
        
        // You can do other operations in the main thread here
        
        // Setting the flag to false to stop the printing thread
        flag = false;
        
        // Waiting for the thread to finish before exiting
        threadObj.join();
        
        return 0;
    }
    
    JonBJ 1 Reply Last reply
    0
    • stackprogramerS stackprogramer

      Hi, my experience in using QThread, I see that QThread vs std::thread is slower, I have a question about how to use them. For managing signal/slots and GUI connection I need QThread, but it is slow for heavy computing use....is It possible to define a std::thread on QThread with a while flag...I want to manage of standard thread with QThread.... (starting and stopping)
      Can anyone guide or help, thanks in advance

      #include <iostream>
      #include <thread>
      
      class A {
      public:
          void print(bool& flag) {
              while (flag) {
                  std::cout << "Printing..." << std::endl;
                  // You can add more code logic here
                  // ...
              }
          }
      };
      
      int main() {
          bool flag = true;
      
          A obj;
          std::thread threadObj(&A::print, &obj, std::ref(flag));
          
          // You can do other operations in the main thread here
          
          // Setting the flag to false to stop the printing thread
          flag = false;
          
          // Waiting for the thread to finish before exiting
          threadObj.join();
          
          return 0;
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @stackprogramer
      Once a thread is running it should run at whatever speed regardless of how it was created. So what exactly do you claim is "slow"? QThreads (with default run()/exec()) are more intended for receiving Qt signals requesting they do some work rather than "starting and stopping". Qt also has QtConcurrent.

      stackprogramerS 1 Reply Last reply
      0
      • JonBJ JonB

        @stackprogramer
        Once a thread is running it should run at whatever speed regardless of how it was created. So what exactly do you claim is "slow"? QThreads (with default run()/exec()) are more intended for receiving Qt signals requesting they do some work rather than "starting and stopping". Qt also has QtConcurrent.

        stackprogramerS Online
        stackprogramerS Online
        stackprogramer
        wrote on last edited by
        #3

        @JonB qthread is managed with qt event loop, but standard thread is managed with OS, https://stackoverflow.com/questions/40880949/what-is-an-event-loop-in-qt I test two case on qthread there's overload and is slower....I need use std::thread on qthread.

        Christian EhrlicherC JonBJ 2 Replies Last reply
        0
        • stackprogramerS stackprogramer

          @JonB qthread is managed with qt event loop, but standard thread is managed with OS, https://stackoverflow.com/questions/40880949/what-is-an-event-loop-in-qt I test two case on qthread there's overload and is slower....I need use std::thread on qthread.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          QThread is not 'managed' by an event loop. You can use QThread without a spinning event loop but then you can not receive signals from another thread.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • stackprogramerS stackprogramer

            @JonB qthread is managed with qt event loop, but standard thread is managed with OS, https://stackoverflow.com/questions/40880949/what-is-an-event-loop-in-qt I test two case on qthread there's overload and is slower....I need use std::thread on qthread.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @stackprogramer said in can we define a std::thread on QThread:

            @JonB qthread is managed with qt event loop

            No, not per se, only like I said if you use default run()/exec(). And anyway, depending on your usage, this is likely to be negligible, e.g. if your thread does a bit of computation in response to a signal. The thread itself just runs, whether you start it from QThread or std::thread, by the OS. Additionally I mentioned QtConcurrent which does not use an event loop.

            I cannot imagine creating some std::thread inside a QThread is helpful.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              The only place where I could imagine a QThread to be slower than a std::thread is during creation/destruction. Once it is running, there is no difference. The default implementation of QThread starts a QEventLoop. There is a chance that you could start your own event loop inside a std::thread. Though Qt might use calls to check for the corresponding QThread of a QObject. You could certainly emit signals from a std::thread as these are "regular" functions calls. You might have to make sure that your connections are Qt::QueuedConnections.

              Both QThread and std::thread need to use the OSes underlying thread functions to control threads. So, if you want to wrap a std::thread into a QThread, a QThread is already wrapping a std::thread, but without the std::thread in the middle.

              1 Reply Last reply
              0

              • Login

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