Question

I am able to remove this warning, please suggest on below points.

QSocketNotifier: socket notifiers cannot be enabled from another thread

Whenever new request comes to QTcpServer i create new object -- xxx . This object then create thread for this socket & send receive data in dowork_socket() function of yyy object. Inside dowork_socket() function i emit signal signalTcpSocketWriteData() to send data from socket.

Object creating the thread for new socket - xxx (I am not pasting full code):----

yyy * ptr;
QTcpSocket *m_pTcpSocket;

public slots:
    void writeDataSlot( QByteArray data )
    {
        m_pTcpSocket->write( data );
        m_pTcpSocket->flush();
    }

Object in which thread is moved -- yyy (I am not pasting full code) :----

xxx *TcpSocketWrapper_ptr;

signals:
    void  signalTcpSocketWriteData( QByteArray);

public slots:
    void dowork_socket();

Inside above object yyy contructor I am connecting signal signalTcpSocketWriteData to slot writeDataSlot :---

connect(this, SIGNAL(signalTcpSocketWriteData( QByteArray)), TcpSocketWrapper_ptr, SLOT(writeDataSlot( QByteArray )), Qt::QueuedConnection );

dowork function of object yyy :--

void TcpSocketThreadObject::dowork_socket()
{
    QByteArray block;


    block.append(" \n hi again .. !!!");

    emit signalTcpSocketWriteData(block);
}

I am able to send data success fully by above method & above warning is removed.

1> Now my question is suppose two threads are created & both thread simultaneously emit the signal then will there be conflict in running the slot ?
2> Will it cause some critical section problem or events are queued & will not be executed simultaneously ?

Please suggest on this two points.

Was it helpful?

Solution

that seems too complicated. You don't need to create threads to work with sockets. Whatever, if you do this, here's two points needs to be kept in mind:

  • QTcpSocket is Not thread-safe itself
  • as long as you use Qt::QueuedConnection when connecting slot (or at least proper using of implicit connections between different threads do this) all code executing inside connected slot will be execited in object's thread loop. In other words, signals emitted from different thread(s) will be queued.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top