What is the best way to put a class member function into another thread for execution?
-
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.
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. -
@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.htmlPlease post code as text, not pictures.
-
@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.
-
-
@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.
-
@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.
-
@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#createThis 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/ -
@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