0
votes

I'm modifying a large project written in Qt that spawns qthreads when certain requests come. Each of these threads write a QTcpSocket that is created in the main thread and connected to a server.

The thread class has a reference to the QTcpSocket. When needed writes to it, the socket write the data and calls waitForBytesWritten and then return.

The thread then waits on a condition until the readyRead signal is emitted from the socket.

Access on the socket is protected with a mutex.

The problem for me is that I see a lot of "QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread" warnings. This because I'm writing on the socket from another thread (the socket has been created in the main thread).

I know that the best solution should be using signals and slots to write and read but unfortunately the project is very complex and I don't have the time to change this behavior.

What can I do?

1
it shouldn't be too hard to remove direct usage of QTcpSocket instance and use instead signal/slots. Compiler will show you all the places needed to be changed with generated compilation errors.Max Go
You have multiple threads which lock a mutex, write to the socket, wait for a reply and then unlock the mutex -- is that correct? If so it really does sound as if the threading model used by the code is fundamentally wrong. Is this code that is already working and which you have to modify or is it essentially all new code.G.M.

1 Answers

1
votes

You can change thread affinity of your QTcpSocket instance to suppress the warning.

But this is really not the right way, you'd better use signal/slot.