2
votes

I try to build a Multi Threading Game Server used QT,so, every client is a standalone thread based "QThread".now I need to send data to other clients(in other thread) whichs in the same game room. example,When a house-owner closed the game room, the Game Server needs to send to the "room_closed" message to other clients whichs in the same soom, but there is an error :

QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread

ps: I tryed to use the sinals/slots but there is still a error:

QObject: Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0x161764e8), parent's thread is ClientThread(0x16196f10), current thread is QThread(0x14a17278)

what should I do?

2

2 Answers

3
votes

It seems you do not handle QObjects and QThreads properly and Qt complains about it.

I suggest you take a look at Qt documentation:

A quick guide:

  • Each instance of QObject is associated with a QThread (QObject::thread()).
  • You can change its associated thread by using QObject::moveTothread(), but the QObject must not have a parent and the call must be made from the thread the QObject is currently associated with.
  • Some QObject (os sub classes) have functions that cannot be called from another thread than the thread they are associated with. That is why you have your first error about QSocketNotifier.
  • You cannot create a QObject from a thread and give it a parent that is associated with another thread. That is your second error.
-1
votes

You called QTcpSocket::write in multiple therads. I solved the problem with signal/slot.