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. What is the best way to put a class member function into another thread for execution?
Forum Updated to NodeBB v4.3 + New Features

What is the best way to put a class member function into another thread for execution?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.5k Views 1 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.
  • Q Offline
    Q Offline
    Quiccz
    wrote on last edited by Quiccz
    #1

    33ed5460-93a6-4d8d-9b9b-e0ae23e810fb-image.png

    I want to exec my member function in another thread. My function will emit some signals to change ui.
    I tryed QtConcurrent::run(). But the slot won't exec in main thread. even if i connect them in main thread.
    85d18379-d879-47c3-9e00-0acdc7340d6e-image.png
    Use move to thread will move my class to thread, which is not i want. I just want to exec one function.
    just like std::thread() run.

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • Q Quiccz

      33ed5460-93a6-4d8d-9b9b-e0ae23e810fb-image.png

      I want to exec my member function in another thread. My function will emit some signals to change ui.
      I tryed QtConcurrent::run(). But the slot won't exec in main thread. even if i connect them in main thread.
      85d18379-d879-47c3-9e00-0acdc7340d6e-image.png
      Use move to thread will move my class to thread, which is not i want. I just want to exec one function.
      just like std::thread() run.

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

      @Quiccz said in What is the best way to put a class member function into another thread for execution?:

      But the slot won't exec in main thread. even if i connect them in main thread.

      How exactly do you connect?
      You should go for worker object approach, see https://doc.qt.io/qt-6/qthread.html

      Please post code as text, not pictures.

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

      jeremy_kJ 1 Reply Last reply
      5
      • jsulmJ jsulm

        @Quiccz said in What is the best way to put a class member function into another thread for execution?:

        But the slot won't exec in main thread. even if i connect them in main thread.

        How exactly do you connect?
        You should go for worker object approach, see https://doc.qt.io/qt-6/qthread.html

        Please post code as text, not pictures.

        jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by jeremy_k
        #3

        @jsulm said in What is the best way to put a class member function into another thread for execution?:

        Please post code as text, not pictures.

        This post serves no purpose but to emphasize this point. I can't meaningfully quote an image. I can't easily view it with a smaller window or device. If I can't easily read it, it does not exist.

        For a program written in plain text, post code in plain text.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • Q Quiccz has marked this topic as solved on
        • Q Quiccz

          33ed5460-93a6-4d8d-9b9b-e0ae23e810fb-image.png

          I want to exec my member function in another thread. My function will emit some signals to change ui.
          I tryed QtConcurrent::run(). But the slot won't exec in main thread. even if i connect them in main thread.
          85d18379-d879-47c3-9e00-0acdc7340d6e-image.png
          Use move to thread will move my class to thread, which is not i want. I just want to exec one function.
          just like std::thread() run.

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

          @Quiccz this is a recipe for disaster, and I'm not only talking about the image.

          • Those are not proper function pointers in your connect calls
          • sender and receiver are properly switched
          • The class is missing a constructor and base class initialisation
          • an endless while loop emitting millions of signals per seconds

          I'm almost certain you don't need threads. At least with your so far shown expertise with c++ and Qt, it is going to cause way more problems than it will solve.


          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.

          Q 1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            @Quiccz this is a recipe for disaster, and I'm not only talking about the image.

            • Those are not proper function pointers in your connect calls
            • sender and receiver are properly switched
            • The class is missing a constructor and base class initialisation
            • an endless while loop emitting millions of signals per seconds

            I'm almost certain you don't need threads. At least with your so far shown expertise with c++ and Qt, it is going to cause way more problems than it will solve.

            Q Offline
            Q Offline
            Quiccz
            wrote on last edited by
            #5

            @J-Hilk I apologize for the confusion. What I wrote was just a piece of pseudo-code to express my logic, and it wasn't intended to be executed.

            I try QThread finished my job.

            #pragma once
            #include <QThread>
            #include <QDateTime>
            class RegionManagerThread : public QThread
            {
            Q_OBJECT
            public:
            	RegionManagerThread(QVector<bool>* stop_vec, QVector<int> durations, int duration, qint64 start_time);
            	void run() override;
            signals:
            	void preload();
            	void finished();
            	void stop(int i);
            
            private:
            	int m_duration;
            	qint64 m_start_time;
            	QVector<int> region_duration;
            	QVector<bool>* m_region_stoped;
            }; 
            
            #include "region_manager_thread.h"
            RegionManagerThread::RegionManagerThread(QVector<bool>* stop_vec, QVector<int> durations, int duration, qint64 start_time)
                : m_region_stoped(stop_vec),
                region_duration(durations),
                m_duration(duration),
                m_start_time(start_time)
            {
            
            }
            
            void RegionManagerThread::run()
            {
                bool preloaded = false;
            
            
                while (1)
                {
                    if (!preloaded && QDateTime::currentDateTime().toMSecsSinceEpoch() - m_start_time >= m_duration - 4000)
                    {
                        preloaded = true;
                        emit preload();
                    }
                    if (QDateTime::currentDateTime().toMSecsSinceEpoch() - m_start_time >= m_duration)
                    {
                        emit finished();
                        return;
                    }
                    for (int i = 0; i < region_duration.size(); i++)
                    {
                        if (!(*m_region_stoped)[i] && QDateTime::currentDateTime().toMSecsSinceEpoch() - m_start_time >= region_duration[i])
                        {
                            emit stop(i);
                        }
                    }
            
                    std::this_thread::sleep_for(std::chrono::milliseconds(2));
                }
            }
            

            However, assigning a member function from another class in the constructor feels inelegant to me. My idea is to run the "run" function as a member function of a class and only place it in another thread for execution, rather than the entire class.

            J.HilkJ 1 Reply Last reply
            0
            • Q Quiccz

              @J-Hilk I apologize for the confusion. What I wrote was just a piece of pseudo-code to express my logic, and it wasn't intended to be executed.

              I try QThread finished my job.

              #pragma once
              #include <QThread>
              #include <QDateTime>
              class RegionManagerThread : public QThread
              {
              Q_OBJECT
              public:
              	RegionManagerThread(QVector<bool>* stop_vec, QVector<int> durations, int duration, qint64 start_time);
              	void run() override;
              signals:
              	void preload();
              	void finished();
              	void stop(int i);
              
              private:
              	int m_duration;
              	qint64 m_start_time;
              	QVector<int> region_duration;
              	QVector<bool>* m_region_stoped;
              }; 
              
              #include "region_manager_thread.h"
              RegionManagerThread::RegionManagerThread(QVector<bool>* stop_vec, QVector<int> durations, int duration, qint64 start_time)
                  : m_region_stoped(stop_vec),
                  region_duration(durations),
                  m_duration(duration),
                  m_start_time(start_time)
              {
              
              }
              
              void RegionManagerThread::run()
              {
                  bool preloaded = false;
              
              
                  while (1)
                  {
                      if (!preloaded && QDateTime::currentDateTime().toMSecsSinceEpoch() - m_start_time >= m_duration - 4000)
                      {
                          preloaded = true;
                          emit preload();
                      }
                      if (QDateTime::currentDateTime().toMSecsSinceEpoch() - m_start_time >= m_duration)
                      {
                          emit finished();
                          return;
                      }
                      for (int i = 0; i < region_duration.size(); i++)
                      {
                          if (!(*m_region_stoped)[i] && QDateTime::currentDateTime().toMSecsSinceEpoch() - m_start_time >= region_duration[i])
                          {
                              emit stop(i);
                          }
                      }
              
                      std::this_thread::sleep_for(std::chrono::milliseconds(2));
                  }
              }
              

              However, assigning a member function from another class in the constructor feels inelegant to me. My idea is to run the "run" function as a member function of a class and only place it in another thread for execution, rather than the entire class.

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

              @Quiccz well it's somewhat better 🙈

              anyway to your original question:

              What is the best way to put a class member function into another thread for execution

              The answer is:
              https://doc.qt.io/qt-6/qthread.html#create

              This is literally the purpose of that function. A QTfied wrapper around C++11’s std::thread

              The this original post about it by KDAB for more detailed explanation:
              https://www.kdab.com/new-qt-5-10-qthread-create/


              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.

              Q 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @Quiccz well it's somewhat better 🙈

                anyway to your original question:

                What is the best way to put a class member function into another thread for execution

                The answer is:
                https://doc.qt.io/qt-6/qthread.html#create

                This is literally the purpose of that function. A QTfied wrapper around C++11’s std::thread

                The this original post about it by KDAB for more detailed explanation:
                https://www.kdab.com/new-qt-5-10-qthread-create/

                Q Offline
                Q Offline
                Quiccz
                wrote on last edited by
                #7

                @J-Hilk The result is same as QtConcurrent::run(). when i connect preload with QueuedConnection in main thread, then emit preload() in new thread. log shows me slot run in the new thread.

                [thread 28644][scenes_widget.cpp:224][Play][debug] : here is main thread
                [thread 33724][scenes_widget.cpp:345][ManagerRegions][info] : manager start
                [thread 33724][program_widget.cpp:160][_PreLoad][info] : preload next
                
                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