QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
来源:互联网 发布:sybase数据库12.5 编辑:程序博客网 时间:2024/05/22 08:12
跨线程使用socket的问题
"跨线程使用socket的问题"相关的详细问题如下: 看到有人说QTcpSocket不能跨线程使用(没在文档上看到这样的描述)但我遇到的问题是可以跨线程读数据,却不能写数据,如下:
void ReciveWorker::readPendingDatagrams(){qDebug() << "ReciveWorker thread id:" << QThread::currentThreadId();QByteArray by = Conn->socket()->readAll();qDebug() << "ReciveWorker recive data:" << by;Conn->socket()->write(by);}主线程创建的socket,在worker线程中read没问题,write时失败,并输出一行错误信息;QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
本来还有一个线程单独负责在这个socket上write,发现不能发送数据。结果发现在可以read的线程里也不能write。这是什么道理?
worker线程类添加一个写的信号(假设是 void sendData(const QByteArray&))
创建worker线程的时候这个写的信号和主线程的槽函数绑定(假设是void sockSend(const QByteArray&))
然后在void sockSend(const QByteArray&) 写发送给socket的方法,之后子线程worker要是想write数据就直接调用
QByteArray data = "Data Fusion";
emit sendData(data);
就行了
//如果有多个子线程同时发送信号给主线程的话,主线程会阻塞住,直接挂掉?
其实,问题的关键就是:线程中创建的对象就必须在线程中释放.
PS: 经shiroki的指正,其实QT有更好的机制来释放对象.那就是deleteLater(). 于是,事情就显得非常简单
阅读全文
0 0
- QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
- QT中解决QSocketNotifier: socket notifiers cannot be enabled from another thread错误
- [pyQt_Error]QObject::killTimer: timers cannot be stopped from another thread
- The service cannot be started, either because it is disabled or because it has no enabled deices ass
- 一个疑惑的问题:QObject::killTimer: Timers cannot be stopped from another thread
- ORB-SLAM2:QObject::~QObject: Timers cannot be stopped from another thread
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled解决方法。
- 烦人的 One or more breakpoints cannot be set and have been disabled.……
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled解决方法
- 于vc调试出现One or more breakpoints cannot be set and have been disabled
- (zz)(VC 6++) One or more breakpoints cannot be set and have been disabled
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled解决方法
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled
- vc调试出现One or morebreakpoints cannot be set and have been disabled
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled解决方法
- One or more breakpoints cannot be set and have been disabled
- 关于vc调试出现One or more breakpoints cannot be set and have been disabled
- One or more breakpoints cannot be set and have been disabled...
- Thinking in Java笔记:Runtime Type Information
- noip2007初赛总结
- 安卓开发日记--2017.10.5
- JVM垃圾回收原理,算法及各种回收器比较
- JavaWeb-JDBC-数据连接池
- QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
- MyEclipse 10.7.1 导出war包出错
- 一张图讲解对象锁和关键字synchronized修饰方法(代码块)
- 标准套接字SOCKET原理及编程(1)
- 多线程之线程池
- java笔记(十一):Collections
- 区间相关问题的学习: 最多不相交区间问题,区间选点问题与区间覆盖问题
- 运算符优先级
- Java根据SVN版本号生成H5游戏资源版本