[Qt-interest] Trouble using a QTcpScoket in more then one thread

Michael Simpson michaelsimpson at mac.com
Tue Dec 1 05:19:45 CET 2009


The way to do this is to queue the data for writing to the thread that creates the sockets.  That thread then writes the data.  The docs explicitly say that sockets are not to be used from threads that did not create them.

I have successfully done this several times.

I.E.  Your dataReady, uses a QMutex to control access to a QList<QByteArray>.  QByteArray is implicitly shared and is thread safe.

In the thread that created the socket object, pull the QByteArray off the list and write it.

Michael

On Nov 30, 2009, at 7:39 PM, Bill Reece wrote:

> I have an application that is really a server. There are several user threads representing users logged on via a QTCPSocket and there are several worker threads each processing data on a unique data model.   Now each process thread has a list of sockets that are subscribed to it and in each workter thread I write to the user socket. Something like:
> 
> WorkThread::dataReady(QByteArray &byteArray)
> {
>     QListIterator <QTcpSocket *> iter(socketList);
>     while(iter.hasNext())
>      socket = iter.next();
>      socket->write(byteArray);
>      // without this line below the client doest get any data
>      socket->flush();
>     // with the line above I get the following run time message:
>    // "QSocketNotifier: socket notifiers cannot be enabled from another thread"
> }
> 
> I would think sockets could be shared by different threads, but Qt seems to not like it. Any thoughts on the best way to do this without the warning messages is appreciated.
> 
> 
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091130/2ecace5b/attachment.html 


More information about the Qt-interest-old mailing list