firemail

标题: QSocketNotifier: Socket notifiers cannot be enabled or disabled from another ... [打印本页]

作者: Qter    时间: 2018-3-11 22:40
标题: QSocketNotifier: Socket notifiers cannot be enabled or disabled from another ...
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread


主线程中不能调用网络请求,把网络请求放线程中。

作者: Qter    时间: 2018-3-24 14:03
跟单例的实现方法也有关系,如下两个,上面会报这个错,下面正常
  1.     static QSharedPointer<QControlSo>& instance()
  2.     {

  3.         if (m_pInstance.isNull())
  4.         {
  5.             QMutexLocker mutexLocker(&m_Mutex);
  6.             if (m_pInstance.isNull())
  7.                 m_pInstance = QSharedPointer<QControlSo>(new QControlSo());
  8.         }
  9.         return m_pInstance;
  10.     }
复制代码
下面的正确
  1. static QControlSo &instance(void)
  2.         {
  3.     #ifdef Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
  4.             if(!QAtomicPointer<QControlSo>::isTestAndSetNative())//运行时进行检测
  5.                 qDebug() << "Error: don's support TestAndSetNative!!!!!!";
  6.     #endif
  7.             //双重检测加锁
  8.             if(m_pInstance.testAndSetOrdered(0,0)){
  9.                 QMutexLocker locker(&m_Mutex);
  10.                 m_pInstance.testAndSetOrdered(0, new QControlSo);
  11.             }
  12.             return * m_pInstance;
  13.         }

  14. private://下与上相同
  15.     QControlSo();
  16.     QControlSo(const QControlSo &);
  17.     QControlSo & operator=(const QControlSo &);
  18.     QReadWriteLock internalMutex;
  19.     static QMutex m_Mutex;
  20.     static QAtomicPointer<QControlSo> m_pInstance;

  21. QMutex QControlSo::m_Mutex;
  22. QAtomicPointer<QControlSo> QControlSo::m_pInstance;
复制代码





欢迎光临 firemail (http://www.firemail.wang:8088/) Powered by Discuz! X3