diff options
author | Bradley T. Hughes <bradley.hughes@nokia.com> | 2009-10-09 10:13:47 (GMT) |
---|---|---|
committer | Bradley T. Hughes <bradley.hughes@nokia.com> | 2009-10-12 07:16:06 (GMT) |
commit | 90a082c9076f35dcca092ade019891e92692710e (patch) | |
tree | f35f8333b84fd8c2323d596eacef71a88749bffd /tests/auto/quuid/tst_quuid.cpp | |
parent | 52aef13521af2137db15ee878893f5c5150471e5 (diff) | |
download | Qt-90a082c9076f35dcca092ade019891e92692710e.zip Qt-90a082c9076f35dcca092ade019891e92692710e.tar.gz Qt-90a082c9076f35dcca092ade019891e92692710e.tar.bz2 |
QUuid::createUuid() not unique when using threads on Unix
QUuid::createUuid() only seeds the PRNG on the first entry, but since
it's using qsrand() and qrand(), all other threads will use the
default seed, and thus generate the exact same UUIDs.
Fix this by adding an internal function (qsrand() overload with no
args) which seeds the PRNG if it hasn't been done already, and use a
seed that is based on current time, a stack address and a global
serial counter (so that the chances of 2 threads using the same seed
are as low as possible).
Task-number: QTBUG-3543
Reviewed-by: Marius Storm-Olsen
Diffstat (limited to 'tests/auto/quuid/tst_quuid.cpp')
-rw-r--r-- | tests/auto/quuid/tst_quuid.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/auto/quuid/tst_quuid.cpp b/tests/auto/quuid/tst_quuid.cpp index e262be7..d78fda5 100644 --- a/tests/auto/quuid/tst_quuid.cpp +++ b/tests/auto/quuid/tst_quuid.cpp @@ -72,6 +72,8 @@ private slots: void variants(); void versions(); + void threadUniqueness(); + public: // Variables QUuid uuidA; @@ -169,6 +171,30 @@ void tst_QUuid::versions() QVERIFY( NCS.version() == QUuid::VerUnknown ); } +class UuidThread : public QThread +{ +public: + QUuid uuid; + + void run() + { + uuid = QUuid::createUuid(); + } +}; + +void tst_QUuid::threadUniqueness() +{ + QVector<UuidThread *> threads(qMax(2, QThread::idealThreadCount())); + for (int i = 0; i < threads.count(); ++i) + threads[i] = new UuidThread; + for (int i = 0; i < threads.count(); ++i) + threads[i]->start(); + for (int i = 0; i < threads.count(); ++i) + QVERIFY(threads[i]->wait(1000)); + for (int i = 1; i < threads.count(); ++i) + QVERIFY(threads[0]->uuid != threads[i]->uuid); + qDeleteAll(threads); +} QTEST_MAIN(tst_QUuid) #include "tst_quuid.moc" |