0

I did serious mistake when I was doing some project and now I have to re-program it from bottom.. I'm writing multi-threaded server which will handle connections, requests, etc.. But, when I created new class with base object of QThread, I was starting thread with QThread->run(), not QThread->start(). Now I get bunch of errors. Here's situation. I have server, which creates new threads on each connection and starts reading data from each connected client.When I receive data, it emits signals which is connected to main GUI class and displays result OK. It also saves socket handle for replying messages. When I write data to socket from GUI class, it says this:

QObject: Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0x23a6c38), parent's thread is MyThread(0x1fb8670), current thread is QThread(0x9eb980)

What's wrong?

Nika
  • 1,864
  • 3
  • 23
  • 44
  • 1
    You CAN'T start thread with run method. If you use run method you didn't start the thread, you just called regular method in current thread context. Take a look at threaded fortune server http://doc.qt.digia.com/qt/network-threadedfortuneserver.html it may be helpful – Kamil Klimek Jan 08 '13 at 08:56
  • If you are using Qt 4, the `QThread` documentation is unfortunately misleading and outdated and should have been updated years ago. Luckily, they finally provided [a much more accurate documentation](http://qt-project.org/doc/qt-5.0/qtcore/qthread.html#details) along with Qt5 which I highly recommend reading as it also applies to Qt 4. The threadedfortuneserver example linked by Kamil Klimek already follows the Qt 5 guidelines. – Tim Meyer Jan 08 '13 at 12:13
  • Then I should check threaded fortune server to deeply understand how the flow works. It'll hard for me, but I will give a try. Thank you. – Nika Jan 08 '13 at 16:06
  • 1
    format your question. it's hard to read – Sam I am says Reinstate Monica Jan 08 '13 at 21:47

1 Answers1

1

I know QT but am not intimately familiar with the QT socket implementation itself, but from the sound of your error message and the way you describe your design, your problem is not related to QT but to thread/socket architecture in general. Multi-threaded applications requiew careful thought intelligent design to be efficient and ensure that your session data etc is consistent and not fraught with concurrency problems, orphaned data structures etc.

It also saves socket handle for replying messages. When I write data to socket from GUI class, it says this...:

Without knowing all the details of your implementation, if you persisted the socket handle in thread 1 to respond to a call in thread 1, and then tried to write data to it from a call using thread 2, that original socket handle is invalid in the context of thread 2. This may explain your error message.

Persisting a socket handle for subsequent calls is not a good way to do things for any number of reasons (including the problem you are encountering.) Why are you doing that? If you need to persist client information across calls, use session structures to persist that data. If you want to keep the whole thread alive while the conversation back and forth continues, design your thread/socket interactions to behave that way and consider a thread pool, etc. But you should not have orphaned sockets just hanging around - each must exist in its own thread context and has no valid handle outside of its 'home' thread.

When a socket is finished with its conversation, the thread that was spawned and the socket created in that thread to continue the conversation are DONE - it should ALL be cleaned up.

If I understand your implementation correctly, my suggestions should fix your problem.

In your comment you mention:

"socket as a global variable" - you should NOT be using a global socket in such a way, if I understand correctly what you're doing.

Your server app should have a main thread with a persistent socket that listens for requests, when you get a request coming in there should be a signal emitted and the slot for that signal should spawn a thread that initializes a new socket for handing that request. Handle the request in the thread, then clean everything up, etc.

If you need to extract information from incoming requests and persist it somewhere, do it in the main thread and other threads should have no access to the structures you use to persist for that session in the main thread (unless you use synchronization mechanisms) Same thing if you want to get information out of the request processing when it's done - get it back out of the spawned thread into the main thread through a signal emitted when the thread terminates.

If you don't follow these rules, you need to use synchronization objects or you'll have all kinds of problems, including what you're experiencing, judging from the sound of your error message.

In short: "What happens in a thread, stays in a thread"

Vector
  • 10,879
  • 12
  • 61
  • 101
  • Hi, the main problem is that I'm new to Qt and I even don't understand what I'm doing sometimes. Now imagine following thing. I have QTcpSocket *socket as a global variable, which can be accessed from any class and will have same result. When new connection arrives, socket = new connection socket. When I'm trying to write data from another class to this socket, it just gives me that error. – Nika Jan 08 '13 at 08:22
  • "socket as a global variable" - you should NOT be using a global socket in such a way, if I understand correctly what you're doing. – Vector Jan 08 '13 at 15:47
  • Your server app should have a main thread with a persistent socket that listens for requests, when you get a request coming in there should be a signal emitted and the slot for that signal should spawn a thread that initializes a new socket for handing that request. Handle the request in the thread, then clean everything up, etc. – Vector Jan 08 '13 at 15:56
  • If you need to extract information from incoming requests and persist it somewhere, do it in the main thread and other threads should have no access to the structures you use to persist in the main thread. Same thing if you want to get information out of the request processing when it's done - get it back out of the spawned thread into the main thread through a signal emitted when the thread terminates. If you don't follow these rules, you need to use synchronization objects or you'll have all kinds of problems, including what you're experiencing, judging from the sound of your error message. – Vector Jan 08 '13 at 15:59
  • In short - I know QT but am not intimately familiar with the QT socket implementation itself, but from the sound of your error message and the way you describe your design, your problem is not related to QT but to thread/socket architecture in general. HTH – Vector Jan 08 '13 at 16:01
  • I just tried it on 1 client, for more clients, I have QList of QTcpSocket, where all the sockets are saved. I don't really understand what else should I do, little sample code will be enough for me. I'm really sorry, I'm just rookie. Can you give me the right point where can I learn that things? Thank you. – Nika Jan 08 '13 at 16:04
  • Read my edit of the answer. I don't have code using QT to provide you with an example. But that doesn't matter. The model I outlined is universal. You should be able to find examples of a 'simple socket server' using QT or just read through the documentation on threads and sockets and I hope what I'm saying will make sense. – Vector Jan 08 '13 at 16:09
  • Thank you, will look the samples now. – Nika Jan 08 '13 at 16:10
  • @Ovér Flôwz: "What happens in a thread, stays in a thread" – Vector Jan 08 '13 at 17:50