PDA

View Full Version : QThread: Move owner of thread to thread with moveToThread



SepticInsect
3rd September 2012, 13:48
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!

mvuori
3rd September 2012, 18:13
I would be _very_ surprised if it would be ok.

yeye_olive
3rd September 2012, 22:49
Hi,
is it ok to move the object owning the thread to the thread?

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.

ChrisW67
3rd September 2012, 23:33
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/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

SepticInsect
4th September 2012, 10:00
Thanks for the help! I will try the example in the link you sent.

SepticInsect
10th September 2012, 16:01
I have implemented my thread according to: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-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?

ChrisW67
11th September 2012, 00:36
It depends on what your thread is doing, but something like:


class Worker : public QObject {
Q_OBJECT
public:
Worker(): stopRequested(false) { }
~Worker() { }

public slots:
void process() {
while (1) {
sleep(1); // fake some uninteruptable work unit

QMutexLocker lock(&mutex);
if (stopRequested)
break;
}
emit finished();
}

public:
void requestStop() {
QMutexLocker lock(&mutex);
stopRequested = true;
}

signals:
void finished();
private:
QMutex mutex;
bool stopRequested;
};

requestStop() can be called to schedule an orderly shut down of the thread.

SepticInsect
11th September 2012, 16:58
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?


//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.

}

ChrisW67
11th September 2012, 22:27
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.

SepticInsect
12th September 2012, 09:22
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.

wysota
12th September 2012, 10:26
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.

So when all threads are finished(), check if cancel was pressed, and if so, quit the application.

SepticInsect
12th September 2012, 16:25
Thanks everyone for the help! The problem is solved!