本文整理汇总了C++中QAtomicInt::testAndSetRelaxed方法的典型用法代码示例。如果您正苦于以下问题:C++ QAtomicInt::testAndSetRelaxed方法的具体用法?C++ QAtomicInt::testAndSetRelaxed怎么用?C++ QAtomicInt::testAndSetRelaxed使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QAtomicInt
的用法示例。
在下文中一共展示了QAtomicInt::testAndSetRelaxed方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void run()
{
testsTurn.release();
threadsTurn.acquire();
QVERIFY(!recursiveMutex.tryLock());
testsTurn.release();
threadsTurn.acquire();
QVERIFY(recursiveMutex.tryLock());
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(recursiveMutex.tryLock());
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
recursiveMutex.unlock();
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
recursiveMutex.unlock();
testsTurn.release();
threadsTurn.acquire();
QTime timer;
timer.start();
QVERIFY(!recursiveMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() >= waitTime);
QVERIFY(!recursiveMutex.tryLock(0));
testsTurn.release();
threadsTurn.acquire();
timer.start();
QVERIFY(recursiveMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() <= waitTime);
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(recursiveMutex.tryLock(waitTime));
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
recursiveMutex.unlock();
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
recursiveMutex.unlock();
testsTurn.release();
threadsTurn.acquire();
QVERIFY(!recursiveMutex.tryLock(0));
QVERIFY(!recursiveMutex.tryLock(0));
testsTurn.release();
threadsTurn.acquire();
timer.start();
QVERIFY(recursiveMutex.tryLock(0));
QVERIFY(timer.elapsed() < waitTime);
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(recursiveMutex.tryLock(0));
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
recursiveMutex.unlock();
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
recursiveMutex.unlock();
testsTurn.release();
threadsTurn.acquire();
}
示例2: 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();
}
示例3: tryLock
void tst_QMutex::tryLock()
{
// test non-recursive mutex
{
class Thread : public QThread
{
public:
void run()
{
testsTurn.release();
// TEST 1: thread can't acquire lock
threadsTurn.acquire();
QVERIFY(!normalMutex.tryLock());
testsTurn.release();
// TEST 2: thread can acquire lock
threadsTurn.acquire();
QVERIFY(normalMutex.tryLock());
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(!normalMutex.tryLock());
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
normalMutex.unlock();
testsTurn.release();
// TEST 3: thread can't acquire lock, timeout = waitTime
threadsTurn.acquire();
QTime timer;
timer.start();
QVERIFY(!normalMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() >= waitTime);
testsTurn.release();
// TEST 4: thread can acquire lock, timeout = waitTime
threadsTurn.acquire();
timer.start();
QVERIFY(normalMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() <= waitTime);
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
timer.start();
// it's non-recursive, so the following lock needs to fail
QVERIFY(!normalMutex.tryLock(waitTime));
QVERIFY(timer.elapsed() >= waitTime);
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
normalMutex.unlock();
testsTurn.release();
// TEST 5: thread can't acquire lock, timeout = 0
threadsTurn.acquire();
QVERIFY(!normalMutex.tryLock(0));
testsTurn.release();
// TEST 6: thread can acquire lock, timeout = 0
threadsTurn.acquire();
timer.start();
QVERIFY(normalMutex.tryLock(0));
QVERIFY(timer.elapsed() < waitTime);
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
QVERIFY(!normalMutex.tryLock(0));
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
normalMutex.unlock();
testsTurn.release();
// TEST 7 overflow: thread can acquire lock, timeout = 3000 (QTBUG-24795)
threadsTurn.acquire();
timer.start();
QVERIFY(normalMutex.tryLock(3000));
QVERIFY(timer.elapsed() < 3000);
normalMutex.unlock();
testsTurn.release();
threadsTurn.acquire();
}
};
Thread thread;
thread.start();
// TEST 1: thread can't acquire lock
testsTurn.acquire();
normalMutex.lock();
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
threadsTurn.release();
// TEST 2: thread can acquire lock
testsTurn.acquire();
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
normalMutex.unlock();
threadsTurn.release();
// TEST 3: thread can't acquire lock, timeout = waitTime
testsTurn.acquire();
normalMutex.lock();
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
threadsTurn.release();
// TEST 4: thread can acquire lock, timeout = waitTime
testsTurn.acquire();
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
normalMutex.unlock();
//.........这里部分代码省略.........
示例4: tryReadLock
void tst_QReadWriteLock::tryReadLock()
{
QReadWriteLock rwlock;
QVERIFY(rwlock.tryLockForRead());
rwlock.unlock();
QVERIFY(rwlock.tryLockForRead());
rwlock.unlock();
rwlock.lockForRead();
rwlock.lockForRead();
QVERIFY(rwlock.tryLockForRead());
rwlock.unlock();
rwlock.unlock();
rwlock.unlock();
rwlock.lockForWrite();
QVERIFY(!rwlock.tryLockForRead());
rwlock.unlock();
// functionality test
{
class Thread : public QThread
{
public:
void run()
{
testsTurn.release();
threadsTurn.acquire();
QVERIFY(!readWriteLock.tryLockForRead());
testsTurn.release();
threadsTurn.acquire();
QVERIFY(readWriteLock.tryLockForRead());
lockCount.ref();
QVERIFY(readWriteLock.tryLockForRead());
lockCount.ref();
lockCount.deref();
readWriteLock.unlock();
lockCount.deref();
readWriteLock.unlock();
testsTurn.release();
threadsTurn.acquire();
QTime timer;
timer.start();
QVERIFY(!readWriteLock.tryLockForRead(1000));
QVERIFY(timer.elapsed() >= 1000);
testsTurn.release();
threadsTurn.acquire();
timer.start();
QVERIFY(readWriteLock.tryLockForRead(1000));
QVERIFY(timer.elapsed() <= 1000);
lockCount.ref();
QVERIFY(readWriteLock.tryLockForRead(1000));
lockCount.ref();
lockCount.deref();
readWriteLock.unlock();
lockCount.deref();
readWriteLock.unlock();
testsTurn.release();
threadsTurn.acquire();
}
};
Thread thread;
thread.start();
testsTurn.acquire();
readWriteLock.lockForWrite();
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
threadsTurn.release();
testsTurn.acquire();
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
readWriteLock.unlock();
threadsTurn.release();
testsTurn.acquire();
readWriteLock.lockForWrite();
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
threadsTurn.release();
testsTurn.acquire();
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
readWriteLock.unlock();
threadsTurn.release();
// stop thread
testsTurn.acquire();
threadsTurn.release();
thread.wait();
}
}