本文整理汇总了C++中QReadWriteLock::tryLockForWrite方法的典型用法代码示例。如果您正苦于以下问题:C++ QReadWriteLock::tryLockForWrite方法的具体用法?C++ QReadWriteLock::tryLockForWrite怎么用?C++ QReadWriteLock::tryLockForWrite使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QReadWriteLock
的用法示例。
在下文中一共展示了QReadWriteLock::tryLockForWrite方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void run()
{
testsTurn.release();
threadsTurn.acquire();
if (readWriteLock.tryLockForWrite())
failureCount++;
testsTurn.release();
threadsTurn.acquire();
if (!readWriteLock.tryLockForWrite())
failureCount++;
if (!lockCount.testAndSetRelaxed(0, 1))
failureCount++;
if (!lockCount.testAndSetRelaxed(1, 0))
failureCount++;
readWriteLock.unlock();
testsTurn.release();
threadsTurn.acquire();
if (readWriteLock.tryLockForWrite(1000))
failureCount++;
testsTurn.release();
threadsTurn.acquire();
if (!readWriteLock.tryLockForWrite(1000))
failureCount++;
if (!lockCount.testAndSetRelaxed(0, 1))
failureCount++;
if (!lockCount.testAndSetRelaxed(1, 0))
failureCount++;
readWriteLock.unlock();
testsTurn.release();
threadsTurn.acquire();
}
示例2: tryWriteLock
void tst_QReadWriteLock::tryWriteLock()
{
{
QReadWriteLock rwlock;
QVERIFY(rwlock.tryLockForWrite());
rwlock.unlock();
QVERIFY(rwlock.tryLockForWrite());
rwlock.unlock();
rwlock.lockForWrite();
QVERIFY(!rwlock.tryLockForWrite());
QVERIFY(!rwlock.tryLockForWrite());
rwlock.unlock();
rwlock.lockForRead();
QVERIFY(!rwlock.tryLockForWrite());
rwlock.unlock();
}
{
QReadWriteLock rwlock(QReadWriteLock::Recursive);
QVERIFY(rwlock.tryLockForWrite());
rwlock.unlock();
QVERIFY(rwlock.tryLockForWrite());
rwlock.unlock();
rwlock.lockForWrite();
QVERIFY(rwlock.tryLockForWrite());
QVERIFY(rwlock.tryLockForWrite());
rwlock.unlock();
rwlock.unlock();
rwlock.unlock();
rwlock.lockForRead();
QVERIFY(!rwlock.tryLockForWrite());
rwlock.unlock();
}
// functionality test
{
class Thread : public QThread
{
public:
Thread() : failureCount(0) { }
void run()
{
testsTurn.release();
threadsTurn.acquire();
if (readWriteLock.tryLockForWrite())
failureCount++;
testsTurn.release();
threadsTurn.acquire();
if (!readWriteLock.tryLockForWrite())
failureCount++;
if (!lockCount.testAndSetRelaxed(0, 1))
failureCount++;
if (!lockCount.testAndSetRelaxed(1, 0))
failureCount++;
readWriteLock.unlock();
testsTurn.release();
threadsTurn.acquire();
if (readWriteLock.tryLockForWrite(1000))
failureCount++;
testsTurn.release();
threadsTurn.acquire();
if (!readWriteLock.tryLockForWrite(1000))
failureCount++;
if (!lockCount.testAndSetRelaxed(0, 1))
failureCount++;
if (!lockCount.testAndSetRelaxed(1, 0))
failureCount++;
readWriteLock.unlock();
testsTurn.release();
threadsTurn.acquire();
}
int failureCount;
};
Thread thread;
thread.start();
testsTurn.acquire();
readWriteLock.lockForRead();
lockCount.ref();
threadsTurn.release();
testsTurn.acquire();
lockCount.deref();
readWriteLock.unlock();
threadsTurn.release();
testsTurn.acquire();
readWriteLock.lockForRead();
lockCount.ref();
//.........这里部分代码省略.........