[Interest] Thread stopped, signal ignored

Thiago Macieira thiago.macieira at intel.com
Wed Mar 20 18:21:31 CET 2013


On quarta-feira, 20 de março de 2013 17.53.14, Etienne Sandré-Chardonnal 
wrote:
> Dear all,
> 
> I'm currently using a QThread with a QObject pushed in it using
> moveToThread. The QObject is basically a network server of class
> NetworkServer, it has a close() slot that closes the QTcpServer and
> outputs a "Port closed" message to the console.

First of all, you do not need threads for that. QTcpServer and QTcpSocket work 
just fine in non-blocking mode, their default mode of operation.

Second, make sure that your QTcpServer was moved along with your worker object 
to the thread. That is, make sure that its parent is set to the object. 
Otherwise, you'll be doing really bad things.

For those reasons, I recommend you simply rip out the threading code. You 
don't need it.

> connect(this, SIGNAL(testMessage()), myObject, SLOT(message()));

> emit testMessage();
> serverThread->quit();
> serverThread->wait();
> 
> 
> 
> Finally, the slot MyObject::message() is never executed (no console output)
> I thought that the slot execution would be queued and executed before
> the thread quits, do you know why this doesn't work?

Because your assumption was wrong. The slot execution *was* queued before the 
thread quits. But there's no guarantee that it will get the chance to execute.

QThread::quit() uses a separate mechanism to cause the quitting. It's not an 
event. Therefore, it's not synchronous to the execution of queued slots. Your 
code probably did the following (from the main thread's point of view):

 1) add QMetaCallEvent to the thread's event dispatcher
 2) wake up thread
 3) set "interrupted" flag to true
 4) wake up thread

Depending on the OS scheduling, the thread may or may not be woken up before 
the interrupted flag was set (depending on the CPU, whether the setting of the 
variable is visible). If it was woken up after the flag was set, the slot would 
not be executed.

You have a race condition with your code.

Solution: rip out the threading code.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130320/4f967451/attachment.sig>


More information about the Interest mailing list