PDA

View Full Version : QThreading problem



paolom
17th July 2012, 11:45
Hi,
I've a plugin and in this plugin I need to have a worker thread whit an event loop that get some work to do.



QThread * th = new QThread(this);
m_workerJob = new WorkerJob;
connect(m_workerJob,SIGNAL(stopThread()),th,SLOT(q uit()));
connect(this,SIGNAL(stopThread()),m_workerJob,SIGN AL(stopThread()));
connect(this,SIGNAL(doWork()),m_workerJob,SLOT(doW ork()));
m_workerJob->moveToThread(th);
th->start();


Then, when I close my application, I want to be sure that my workerJob is finished.
I've this code that I call before to close the application:



QThread * th = m_workerJob->thread();
emit stopThread();
th->wait();


This function is blocking. The problem is that the SIGNAL stopThread() is not processed cause it need to return to the event loop to be processed, while the wait() is a blocking function.

I want to wait synchronously the exit of the worker thread. So, how can I solve it??

Thanks

wysota
17th July 2012, 12:33
Connect the stopThread() signal directly to quit() slot of the thread object.

paolom
17th July 2012, 12:40
Yes, it works, but I lost the astraction that the workerJob give to me.

Howere, If there isn't any other solution, I get this.

wysota
17th July 2012, 13:08
There are many solutions but the one you had was wrong because it involved cross-thread slot invocation. An alternative solution is to call wait() on the thread once you receive a signal that the thread has finished execution.