can we define a std::thread on QThread
-
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; }
-
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; }
@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"?QThread
s (with defaultrun()
/exec()
) are more intended for receiving Qt signals requesting they do some work rather than "starting and stopping". Qt also has QtConcurrent. -
@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"?QThread
s (with defaultrun()
/exec()
) are more intended for receiving Qt signals requesting they do some work rather than "starting and stopping". Qt also has QtConcurrent.@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.
-
@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.
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.
-
@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.
@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 fromQThread
orstd::thread
, by the OS. Additionally I mentionedQtConcurrent
which does not use an event loop.I cannot imagine creating some
std::thread
inside aQThread
is helpful. -
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::QueuedConnection
s.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.