Qt Socket blocking functions required to run in QThread where created. Any way past this?

Posted by Alexander Kondratskiy on Stack Overflow See other posts from Stack Overflow or by Alexander Kondratskiy
Published on 2011-03-02T22:34:19Z Indexed on 2011/03/02 23:24 UTC
Read the original article Hit count: 328

Filed under:
|
|

The title is very cryptic, so here goes!

I am writing a client that behaves in a very synchronous manner. Due to the design of the protocol and the server, everything has to happen sequentially (send request, wait for reply, service reply etc.), so I am using blocking sockets. Here is where Qt comes in.

In my application I have a GUI thread, a command processing thread and a scripting engine thread. I create the QTcpSocket in the command processing thread, as part of my Client class. The Client class has various methods that boil down to writing to the socket, reading back a specific number of bytes, and returning a result.

The problem comes when I try to directly call Client methods from the scripting engine thread. The Qt sockets randomly time out and when using a debug build of Qt, I get these warnings:

QSocketNotifier: socket notifiers cannot be enabled from another thread
QSocketNotifier: socket notifiers cannot be disabled from another thread

Anytime I call these methods from the command processing thread (where Client was created), I do not get these problems.

To simply phrase the situation:

Calling blocking functions of QAbstractSocket, like waitForReadyRead(), from a thread other than the one where the socket was created (dynamically allocated), causes random behaviour and debug asserts/warnings.

Anyone else experienced this? Ways around it?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt