summaryrefslogtreecommitdiffstats
path: root/tests/auto/qpointer
diff options
context:
space:
mode:
authorOlivier Goffart <olivier.goffart@nokia.com>2010-12-10 12:20:48 (GMT)
committerOlivier Goffart <olivier.goffart@nokia.com>2010-12-13 18:15:54 (GMT)
commit18e3cd65980e1bc01e6af4807cae0bceca25288c (patch)
tree66c1f5f6078782aba6cfb1eb467d13bd6f5df55f /tests/auto/qpointer
parent8e6078401562d40d2e63c4a2c769843088ec3350 (diff)
downloadQt-18e3cd65980e1bc01e6af4807cae0bceca25288c.zip
Qt-18e3cd65980e1bc01e6af4807cae0bceca25288c.tar.gz
Qt-18e3cd65980e1bc01e6af4807cae0bceca25288c.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/auto/qpointer')
-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 5a07e41..ce23764 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"