|
沙发
楼主 |
发表于 2018-3-24 14:03:33
|
只看该作者
跟单例的实现方法也有关系,如下两个,上面会报这个错,下面正常- static QSharedPointer<QControlSo>& instance()
- {
- if (m_pInstance.isNull())
- {
- QMutexLocker mutexLocker(&m_Mutex);
- if (m_pInstance.isNull())
- m_pInstance = QSharedPointer<QControlSo>(new QControlSo());
- }
- return m_pInstance;
- }
复制代码 下面的正确- static QControlSo &instance(void)
- {
- #ifdef Q_ATOMIC_POINTER_TEST_AND_SET_IS_ALWAYS_NATIVE
- if(!QAtomicPointer<QControlSo>::isTestAndSetNative())//运行时进行检测
- qDebug() << "Error: don's support TestAndSetNative!!!!!!";
- #endif
- //双重检测加锁
- if(m_pInstance.testAndSetOrdered(0,0)){
- QMutexLocker locker(&m_Mutex);
- m_pInstance.testAndSetOrdered(0, new QControlSo);
- }
- return * m_pInstance;
- }
- private://下与上相同
- QControlSo();
- QControlSo(const QControlSo &);
- QControlSo & operator=(const QControlSo &);
- QReadWriteLock internalMutex;
- static QMutex m_Mutex;
- static QAtomicPointer<QControlSo> m_pInstance;
- QMutex QControlSo::m_Mutex;
- QAtomicPointer<QControlSo> QControlSo::m_pInstance;
复制代码 |
|