diff options
author | David Faure <david.faure@kdab.com> | 2013-05-19 12:27:54 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-05-19 20:39:32 +0200 |
commit | f22b29162c8689f740841819d3350a3c013d7121 (patch) | |
tree | ef2b445ad9a3616faecbec0259b3d06eeb728f69 /tests/auto/corelib/thread | |
parent | 94c7002df30d931df13b3bae9d4c46ff1e6f714c (diff) |
Fix some data races in tst_qthreadpool.
Detected by helgrind.
Change-Id: I5f73a62610b4bdc7f26629f3dcf7b11467897a5c
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto/corelib/thread')
-rw-r--r-- | tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp index a93a94a470..8a72ae5bce 100644 --- a/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp +++ b/tests/auto/corelib/thread/qthreadpool/tst_qthreadpool.cpp @@ -212,22 +212,22 @@ void tst_QThreadPool::waitcomplete() QCOMPARE(testFunctionCount, runs); } -volatile bool ran; +QAtomicInt ran; // bool class TestTask : public QRunnable { public: void run() { - ran = true; + ran.store(true); } }; void tst_QThreadPool::runTask() { QThreadPool manager; - ran = false; + ran.store(false); manager.start(new TestTask()); - QTRY_VERIFY(ran); + QTRY_VERIFY(ran.load()); } /* @@ -235,19 +235,19 @@ void tst_QThreadPool::runTask() */ void tst_QThreadPool::singleton() { - ran = false; + ran.store(false); QThreadPool::globalInstance()->start(new TestTask()); - QTRY_VERIFY(ran); + QTRY_VERIFY(ran.load()); } -int *value = 0; +QAtomicInt *value = 0; class IntAccessor : public QRunnable { public: void run() { for (int i = 0; i < 100; ++i) { - ++(*value); + value->ref(); QTest::qSleep(1); } } @@ -259,7 +259,7 @@ public: */ void tst_QThreadPool::destruction() { - value = new int; + value = new QAtomicInt; QThreadPool *threadManager = new QThreadPool(); threadManager->start(new IntAccessor()); threadManager->start(new IntAccessor()); @@ -679,8 +679,8 @@ void tst_QThreadPool::tryStart() } QMutex mutex; -int activeThreads = 0; -int peakActiveThreads = 0; +QAtomicInt activeThreads; +QAtomicInt peakActiveThreads; void tst_QThreadPool::tryStartPeakThreadCount() { class CounterTask : public QRunnable @@ -692,14 +692,14 @@ void tst_QThreadPool::tryStartPeakThreadCount() { { QMutexLocker lock(&mutex); - ++activeThreads; - peakActiveThreads = qMax(peakActiveThreads, activeThreads); + activeThreads.ref(); + peakActiveThreads.store(qMax(peakActiveThreads.load(), activeThreads.load())); } QTest::qWait(100); { QMutexLocker lock(&mutex); - --activeThreads; + activeThreads.deref(); } } }; @@ -711,13 +711,13 @@ void tst_QThreadPool::tryStartPeakThreadCount() if (threadPool.tryStart(&task) == false) QTest::qWait(10); } - QCOMPARE(peakActiveThreads, QThread::idealThreadCount()); + QCOMPARE(peakActiveThreads.load(), QThread::idealThreadCount()); for (int i = 0; i < 20; ++i) { if (threadPool.tryStart(&task) == false) QTest::qWait(10); } - QCOMPARE(peakActiveThreads, QThread::idealThreadCount()); + QCOMPARE(peakActiveThreads.load(), QThread::idealThreadCount()); } void tst_QThreadPool::tryStartCount() |