summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorOlivier Goffart <olivier.goffart@nokia.com>2010-12-10 12:20:48 (GMT)
committerQt Commercial Integration <QtCommercial@digia.com>2012-01-31 10:24:39 (GMT)
commit0cc309d2dd0469bc8997154455b91fba4697ccc5 (patch)
tree469a94ee2e295d364b826e6087e991a32ed67b98 /tests
parent35e19b15241777d4339746a8b8d3ecca313cd3cf (diff)
downloadQt-0cc309d2dd0469bc8997154455b91fba4697ccc5.zip
Qt-0cc309d2dd0469bc8997154455b91fba4697ccc5.tar.gz
Qt-0cc309d2dd0469bc8997154455b91fba4697ccc5.tar.bz2
QPointer: thread safety
Fix a race condition between the destructor of QPointer and the destruction of the object living in a different thread. Task-number: QTBUG-16005 Reviewed-by: Joao
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qpointer/tst_qpointer.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/tests/auto/qpointer/tst_qpointer.cpp b/tests/auto/qpointer/tst_qpointer.cpp
index bbc92cf..39637d8 100644
--- a/tests/auto/qpointer/tst_qpointer.cpp
+++ b/tests/auto/qpointer/tst_qpointer.cpp
@@ -73,6 +73,7 @@ private slots:
void castDuringDestruction();
void data() const;
void dataSignature() const;
+ void threadSafety();
};
tst_QPointer::tst_QPointer()
@@ -345,5 +346,36 @@ void tst_QPointer::dataSignature() const
}
}
+class TestRunnable : public QObject, public QRunnable {
+ void run() {
+ QPointer<QObject> obj1 = new QObject;
+ QPointer<QObject> obj2 = new QObject;
+ obj1->moveToThread(thread()); // this is the owner thread
+ obj1->deleteLater(); // the delete will happen in the owner thread
+ obj2->moveToThread(thread()); // this is the owner thread
+ obj2->deleteLater(); // the delete will happen in the owner thread
+ }
+};
+
+void tst_QPointer::threadSafety()
+{
+
+ QThread owner;
+ owner.start();
+
+ QThreadPool pool;
+ for (int i = 0; i < 300; i++) {
+ QPointer<TestRunnable> task = new TestRunnable;
+ task->setAutoDelete(true);
+ task->moveToThread(&owner);
+ pool.start(task);
+ }
+ pool.waitForDone();
+
+ owner.quit();
+ owner.wait();
+}
+
+
QTEST_MAIN(tst_QPointer)
#include "tst_qpointer.moc"