Pergunta

I have multithread server (inherits QTcpServer). When new connection appears, I create new task (inherits QRunnable), passing socket descriptor to constructor and push this task to QThreadpool (have 3 workers).

QThreadPool::globalInstance()->start(task);

In run() I dynamically create QTcpSocket, set socket descriptor and read first received byte. Based on value of this byte I create new specific task (also inherits QRunnable), passing to its ctr pointer to earlier created QTcpSocket object, and also push this task to QThreadpool.

This specific task make some routine and app crashes. From log file, I see destructor of this specific task was called.

Also Qt Creator throws next error message:

QObject: Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0x18c62290), parent's thread is QThread(0x18c603e0), current thread is QThread(0x18cc3b60) QSocketNotifier: socket notifiers cannot be disabled from another thread ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 18cc3b60. Receiver '' (of type 'QNativeSocketEngine') was created in thread 18c603e0", file kernel/qcoreapplication.cpp, line 420

I found similar posts but unfortunately I could not understand how to fix my problem. Please, help me.

Foi útil?

Solução

You cannot use QTcpSocket from two different threads, because QObjects are not thread-safe.

You've created your QTcpSocket in the first task, so it "lives" in the thread associated with that task. If you pass its pointer into another QRunnable, then a second thread will try to access it, which will break things.

You'll need to redesign your app in a way that doesn't share the same QTcpSocket between different threads. One possibility is to implement different specific functions in your original task, and simply select the appropriate function based on the first received byte

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top