Hi,
is it ok to move the object owning the thread to the thread? Like in the example below.
a.h:
QThread* m_pThread;
a.cpp:
m_pThread = new QThread;
this->moveToThread(m_pThread);
Thanks!
Hi,
is it ok to move the object owning the thread to the thread? Like in the example below.
a.h:
QThread* m_pThread;
a.cpp:
m_pThread = new QThread;
this->moveToThread(m_pThread);
Thanks!
I would be _very_ surprised if it would be ok.
In your example the QThread object is not "owned" by anyone (and you have to delete it to avoid a memory leak). Depending on the rest of the code (which we do not have access to), it may or may not be correct. However the design looks bad: if the QThread is only meant to be used internally by the class you define, then it should not affect the object's public behavioural properties such as its thread affinity.
What are you hoping to achieve by this? I cannot see it will end well at delete time with the parent QObject in one thread and the QThread in another.
If you have convinced yourself that your program really needs threads (most don't) then this is a good way to do QThread use and cleanup:
http://mayaposch.wordpress.com/2011/...l-explanation/
SepticInsect (10th September 2012)
Thanks for the help! I will try the example in the link you sent.
I have implemented my thread according to: http://mayaposch.wordpress.com/2011/...l-explanation/ and it works fine.
If I would like to stop my thread from the main thread (when the user presses Cancel) how shall I do that?
It depends on what your thread is doing, but something like:
requestStop() can be called to schedule an orderly shut down of the thread.Qt Code:
Q_OBJECT public: Worker(): stopRequested(false) { } ~Worker() { } public slots: void process() { while (1) { sleep(1); // fake some uninteruptable work unit if (stopRequested) break; } emit finished(); } public: void requestStop() { stopRequested = true; } signals: void finished(); private: QMutex mutex; bool stopRequested; };To copy to clipboard, switch view to plain text mode
OK Thanks.
I have decided that i whould like to wait until may thread has finished instead of exiting the thread when the user presses Cancel. How can i implement that?
Qt Code:
//Slot connected to finished signal of the worker class void a::threadFinished() { m_finished = true; } void a::cancelPressed() { //Here I would like to wait until the thread is finished. bool stopWaiting = false; while(!stopWaiting) { m_mutex.lock(); if (m_finished) stopWaiting = true; m_mutex.unlock(); //How shall I implement the delay (if stopWaiting is false) before i continue another loop in the whileloop without locking the entire program? } //Do som stuff after the thread has finished. }To copy to clipboard, switch view to plain text mode
Both the worker and the QThread issue a finished() signal when they are done. Connect to that signal and put your post-processing in the slot. Then you don't have to block the Qt event loop and GUI update.
But I whould like to exit the entire program when the user presses Cancel. I would like to make sure the threads are finished before I exit the program.
SepticInsect (12th September 2012)
Thanks everyone for the help! The problem is solved!
Bookmarks