ambiguous documentation and behavior of QTcpSocket
Hi There,
I use currently Qt 4.5.2
after a long exasperating time of coding I find some very oppositional statements in Qt Documentation.
1.) Qt Assistant QTcpSocket / QAbstractSocket
Quote:
QTcpSocket hardy no sentence how to with threads
2.) Qt Assistant Threaded Fortune Server Example
Code:
void FortuneThread::run()
{
Quote:
What's worth noticing is that we are creating this object inside the thread, which automatically associates the socket to the thread's event loop. This ensures that Qt will not try to deliver events to our socket from the main thread while we are accessing it from FortuneThread::run().
At this example it seems there ist no need to call "exec()" or, even not to use "exec()" ... will not deliver events to our socket for Main thread !
3.) doc/threads.html and Qthread
Quote:
hreads to slots in this thread, using a mechanism called queued connections. It also makes it possible to use classes that require the event loop, such as QTimer and QTcpSocket, in the thread.
If somebody reads and takes the Threaded Fortune Server Example he will struggle and fail very hard!
On my specific problem I use
Code:
MyThread::run()
{
while (running)
{
m_pFifio->getData( pBuffer, size );
parseData( pData );
... in some Object living in that thread I Call
qint64 len = pTcpSocket->write ( data, length ) ;
}
}
Where write() writes length bytes of data which never will appears on the other side of the socket ( some other (remote ) client programm ).
One step better will be to append a waitForReadyRead() after write
In deed QTcpSocket sends sends some data, but not complete tough write returns the correct amount of bytes.
For me it dosn`t realy make sense that I have to bind QtcpSocket to event loop!
It should go the normal (classic ) way to !
Does sombody find a solution for this way
regards
Re: ambiguous documentation and behavior of QTcpSocket
What exactly is ambiguous? If you are wondering why the threaded fortune server thread doesn't call exec() then I can tell you that is because it uses methods from the "waitFor" family that "emulate" the event loop for a particular socket. On the other hand if you created the socket in the main thread, called exec() on the application (from the main thread) and then called waitFor*() on the socket (or actually any other method) you would be creating a race condition between the two threads. The main thread would try to deliver events for the socket upon data arrival and at the same time you would be waiting for data to arrive in the other thread (in case of waitFor methods) and both these situations couldn't happen or you could be reading data from the socket's buffer at the same time the other thread was trying to write to this buffer taking the socket ot of sync. The documentation is clear about this provided you know what reentrancy is. If you don't then... well... you can only blame yourself, the docs can't explain it in docs for every of the over seven hundred classes in Qt.