QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
-
hi.
excuse me. I am weak in English.
I'm trying to write a program that reads UDP datagrams with separate thread.class NetRecieverThread : public QThread { Q_OBJECT public: QUdpSocket *m_Socket; int m_iPortNum; QString m_stIpAddress; NetRecieverThread(QObject *parent ,QString sAddress ,int PortNum ) { m_iPortNum = PortNum; m_stIpAddress = sAddress; m_Socket = new QUdpSocket (this); bool bl = m_Socket ->bind(QHostAddress(sAddress),PortNum); bl = 0; //connect(m_Socket ,SIGNAL(readyRead()),this,SLOT(readData())); } void run() Q_DECL_OVERRIDE { while(true) { QByteArray datagram; bool ReadyRead = false; while(!ReadyRead) ReadyRead = m_Socket->waitForReadyRead(30000); datagram.resize(m_Socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; int iSize = m_Socket->readDatagram(datagram.data(),datagram.size(),&sender,&senderPort); if(iSize <=0) return; char *data = datagram.data(); quint8 RecData = data[0]; qDebug() <<"\n \n\ NetRecieverThread : Rec Data "<<RecData<<"Thread Information : "<< QThread::currentThread()<<QThread::currentThreadId(); emit resultReady(datagram); } } signals: void resultReady(QByteArray datagram); }; //-------------------------------------------------------------------- //-------------------------------------------------------------------- class NetRecieveManager : public QObject { Q_OBJECT NetRecieverThread *tRec; public: NetRecieveManager() { } void startWorkInAThread() { tRec = new NetRecieverThread(0,"127.0.0.1",56071); connect(tRec, &NetRecieverThread::resultReady, this, &NetRecieveManager::handleResults); tRec->start(); } private slots: void handleResults(QByteArray datagram) { char *ch = datagram.data(); quint8 RecData = ch[0]; qDebug() <<"NetRecieveManager: Rec Data "<<RecData<<"Thread Information : "<< QThread::currentThread()<<QThread::currentThreadId()<<"\n \n "; } }; //-------------------------------------------------------------------- //-------------------------------------------------------------------- int main(int argc, char *argv[]) { QApplication a(argc, argv); //------------------------------------------------------ NetRecieveManager myob; QThread NetMangerThread; NetMangerThread.setObjectName("NetMangerThread"); myob.startWorkInAThread(); myob.moveToThread(&NetMangerThread); NetMangerThread.start(); //------------------------------------------------------ return a.exec(); }
then output program is :
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieverThread : Rec Data 208 Thread Information : NetRecieverThread(0xb625c8) 0xe10 NetRecieveManager: Rec Data 208 Thread Information : QThread(0x12fedc, name = "NetMangerThread") 0xb38 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieverThread : Rec Data 209 Thread Information : NetRecieverThread(0xb625c8) 0xe10 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieveManager: Rec Data 209 Thread Information : QThread(0x12fedc, name = "NetMangerThread") 0xb38 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieverThread : Rec Data 210 Thread Information : NetRecieverThread(0xb625c8) 0xe10 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieveManager: Rec Data 210 Thread Information : QThread(0x12fedc, name = "NetMangerThread") 0xb38 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieverThread : Rec Data 211 Thread Information : NetRecieverThread(0xb625c8) 0xe10 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieveManager: Rec Data 211 Thread Information : QThread(0x12fedc, name = "NetMangerThread") 0xb38 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieverThread : Rec Data 212 Thread Information : NetRecieverThread(0xb625c8) 0xe10 QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread NetRecieveManager: Rec Data 212 Thread Information : QThread(0x12fedc, name = "NetMangerThread") 0xb38
everything is ok. both 2 threads work good. i dont know why get this message
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another threadIs the problem will be.
thanks -
Hi and welcome to devnet,
The constructor of NetRecieverThread is run in the main thread so m_Socket is created in the main thread. On the other hand the run method is called in the thread managed by QThread that's why you get the message.
-
This post is deleted! -
@SGaist
thanks SGait
ok.
i change code and move create m_socket to run function and solved problem.void run() Q_DECL_OVERRIDE { m_Socket = new QUdpSocket (0); bool bl = m_Socket ->bind(QHostAddress(m_stIpAddress),m_iPortNum); while(true) { . . .
-
Don't forget to delete the socket before the end of run otherwise you'll have a memory leak