diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp b/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp index 4093e976072..1d47d986575 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp +++ b/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp @@ -65,6 +65,8 @@ private slots: void uncontended(); void readOnly_data(); void readOnly(); + void writeOnly_data(); + void writeOnly(); // void readWrite(); }; @@ -193,5 +195,66 @@ void tst_QReadWriteLock::readOnly() holder.value(); } +static QString global_string; + +template +void testWriteOnly() +{ + struct Thread : QThread + { + Mutex *lock; + void run() override + { + for (int i = 0; i < Iterations; ++i) { + QString s = QString::number(i); // Do something outside the lock + Locker locker(lock); + global_string = s; + } + } + }; + Mutex lock; + std::vector> threads; + for (int i = 0; i < threadCount; ++i) { + auto t = qt_make_unique(); + t->lock = &lock; + threads.push_back(std::move(t)); + } + QBENCHMARK { + for (auto &t : threads) { + t->start(); + } + for (auto &t : threads) { + t->wait(); + } + } +} + +void tst_QReadWriteLock::writeOnly_data() +{ + QTest::addColumn("holder"); + + // QTest::newRow("nothing") << FunctionPtrHolder(testWriteOnly); + QTest::newRow("QMutex") << FunctionPtrHolder(testWriteOnly); + QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testWriteOnly); + QTest::newRow("std::mutex") << FunctionPtrHolder( + testWriteOnly>>); +#ifdef __cpp_lib_shared_mutex + QTest::newRow("std::shared_mutex") << FunctionPtrHolder( + testWriteOnly>>); +#endif +#if defined __cpp_lib_shared_timed_mutex + QTest::newRow("std::shared_timed_mutex") << FunctionPtrHolder( + testWriteOnly>>); +#endif +} + +void tst_QReadWriteLock::writeOnly() +{ + QFETCH(FunctionPtrHolder, holder); + holder.value(); +} + QTEST_MAIN(tst_QReadWriteLock) #include "tst_qreadwritelock.moc"