[Qt-interest] sockets and threads on linux

Jason H scorp1us at yahoo.com
Tue Feb 15 18:19:41 CET 2011


What he said. And: Realize you don't need to make your socket a member.

Just do:
{
...
QTcpSocket socket;
socket.setSocketDescriptor(m_socketDescriptor);

..
// when thread is done, it will clean up socket.
}

----- Original Message ----
From: "william.crocker at analog.com" <william.crocker at analog.com>
To: qt-interest at trolltech.com
Sent: Tue, February 15, 2011 11:20:59 AM
Subject: Re: [Qt-interest] sockets and threads on linux



pmqt71 wrote:
> I did as you describe, and I expected the same you say. Here is my run 
> method :
>  
> void ClientConnectionThread::run()
> {
>    m_tcpSocket = new QTcpSocket();
>    if (!m_tcpSocket->setSocketDescriptor(m_socketDescriptor)) {
>        emit error(m_tcpSocket->error());
>        return;
>    }
>    connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));

Thats because *this* lives in the main thread.
Only the body of ClientConnectionThread::run lives in the actual thread.

>    connect(m_tcpSocket, SIGNAL(disconnected()), this, 
> SLOT(onSocketDisconnect()));
>    exec();
> }
>  
> but the onReadyRead slot was running in the main thread. I'm pretty 
> sure, but I'll try again and tell you if I'm wrong or not.
> bye
> pm
>  
> 
> 
>  
> 2011/2/15 Jason H <scorp1us at yahoo.com <mailto:scorp1us at yahoo.com>>
> 
>     Um, no.
>     When you've created the QThread subclass, and passed the constructor
>     the int descriptor and store it in a member variable, you've not yet
>     started a thread, but instead created a QThread object. Then in your
>     run() implementation, which is the actual thread, then you create a
>     socket using the member variable int descriptor. No need to use
>     moveToThread().
> 
>     HTH
>     ------------------------------------------------------------------------
>     *From:* pmqt71 <pmqt71 at gmail.com <mailto:pmqt71 at gmail.com>>
>     *To:* "william.crocker at analog.com
>     <mailto:william.crocker at analog.com>" <william.crocker at analog.com
>     <mailto:william.crocker at analog.com>>; qt-interest at trolltech.com
>     <mailto:qt-interest at trolltech.com>
>     *Sent:* Tue, February 15, 2011 7:49:37 AM
> 
>     *Subject:* Re: [Qt-interest] sockets and threads on linux
> 
>      
>     Upgrading Qt to 4.7 solved the problem.
>      
>     The target server was using Qt 4.5.0 shipped on Ubuntu repositories.
>     Just as information, the problem is only with Qt 4.5.0 64 bit, not
>     with Qt 4.5.0 32.
>      
>     thanks to all.
>     pm
>      
>      
>     p.s. Bill, as I experienced, your steps 2 and 3 make the socket live
>     in the main thread rather than in the one you create. I
>     used moveToThread to correctly move the QTcpSocket object to the
>     dedicated thread. See my previous messages where Thiago gave me the
>     right suggestions.
>      
>      
> 
>      
>     2011/2/15 william.crocker at analog.com
>     <mailto:william.crocker at analog.com> <william.crocker at analog.com
>     <mailto:william.crocker at analog.com>>
> 
>         Some detail omitted   :-)
> 
>         1 - Subclass QTcpServer and redefine incomingConnection(int)
>             ( The QTcpServer runs in the main thread. )
> 
>         2 - In this function, create a thread and pass it the int file
>         descriptor.
> 
>         3 - In the ::run method of the thread, create a ::run/local
>         QTcpSocket and
>             setSocketDescriptor with the incoming file desc.
> 
>         4 - Create some other ::run/local object to handle the readyRead()
> 
>         5 - Create and ::exec an event loop.
> 
>         I have a server which has been running flawlessly
>         under this scenario for years.
> 
>         Bill
> 
>          > On Monday, 14 de February de 2011 19:43:34 Francisco Gonzalez
>         wrote:
>          >> In your implementation details both signal readyRead() and slot
>          >> onReadyRead()  are members of your MyTcpSocket class, so
>         when you move the
>          >> pSocket object to the QThread th, you are moving both the
>         signal, and the
>          >> processing function. I do not understand how can it improve
>         the socket
>          >> capability of processing, as both functions live in the same
>         thread.
>          >
>         _______________________________________________
>         Qt-interest mailing list
>        Qt-interest at qt.nokia.com <mailto:Qt-interest at qt.nokia.com>
>        http://lists.qt.nokia.com/mailman/listinfo/qt-interest
> 
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest



      



More information about the Qt-interest-old mailing list