PDA

View Full Version : QThread blocking wait()



bunjee
5th May 2009, 17:20
Hey there,

I've implemented a QThread.

Here is my run function:


void qkReceptor::run()
{
// A timer to avoid high CPU charge
QTimer timer;

connect(&timer, SIGNAL(timeout()), this, SLOT(onCheckStdin()), Qt::DirectConnection);
connect(this, SIGNAL(newLine(QString)), this, SLOT(onNewLine(QString)), Qt::BlockingQueuedConnection);

timer.start(100);

QThread::exec();
}

Here is my stop function:


void qkReceptor::stop()
{
QThread::quit();
QThread::wait(); // Infinite loop here
}

When calling stop from my main thread, wait blocks everything.
Why?

Thanks.

mcosta
5th May 2009, 17:39
The correct way is


void qkReceptor::stop()
{
this->quit();
this->wait(); // Infinite loop here
}

wysota
6th May 2009, 09:21
I'd like to note that wait() should be called not from the thread itself but from a thread that created it (which I guess is what you are trying to do or might be trying to do). The semantics for this call is "block me here until the thread in question has finished its execution". If you call it from the worker thread itself then the thread will be suspended and will never finish thus wait() will never end and you'll have a deadlock.