当前位置: 首页 > news >正文

测试QT读写锁(QReadWriteLock )和互斥锁(QReadWriteLock )的执行效率

上代码:

#include <QCoreApplication>
#include <QElapsedTimer>
#include <QtConcurrent>
#include <QDebug>int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);qSetMessagePattern("(%{time hh:mm:ss.zzz} %{threadid} %{file}:%{line}): \t%{message}");QList<int> list_report;for (int round = 0; round < 5; round++){QMutex *mtx_lock = new QMutex;QReadWriteLock *rw_lock = new QReadWriteLock;QFutureWatcher<void> *watcher = new QFutureWatcher<void>;QSemaphore semaphore;QAtomicInt times = 1000 * 1000; //1mQDateTime datatime;QElapsedTimer timer;timer.start();for (int i = 0; i < 5; i++){QFuture<void> future = QtConcurrent::run([&, i]() {int index = i;int write_times{0};semaphore.release(1);while (times > 0) {int type = QRandomGenerator::global()->bounded(2);if (type == 1 and index == 0) {
//                        QWriteLocker locker(rw_lock);
//                        datatime = QDateTime::currentDateTime();QMutexLocker locker(mtx_lock);datatime = QDateTime::currentDateTime();write_times++;} else {
//                        QReadLocker locker(rw_lock);
//                        auto newdata = datatime;QMutexLocker locker(mtx_lock);auto newdata = datatime;}times--;}qDebug()<<index<<"write:times"<<write_times;});watcher->setFuture(future);}semaphore.acquire(1);watcher->waitForFinished();qDebug() << round << "QFuture (ms)" << timer.elapsed()<<"------------------------------------";list_report.append(timer.elapsed());delete (watcher);delete (rw_lock);delete (mtx_lock);}qDebug()<<"report:"<<list_report;return a.exec();
}

测试结果 :

5线程 (1000  * 1000)
写概率指定线程写结果(单位:ms)平均写入次数
读写锁1/1000(193, 191, 183, 188, 186)200
互斥锁1/1000(111, 112, 109, 105, 106)200
互斥锁1/2(2808, 2790, 2722, 2719, 2800)100000
互斥锁1/5(1160, 1133, 1142, 1163, 1137)40000
读写锁1/5(4627, 4689, 4614, 4524, 4617)40000
互斥锁1/2(321, 303, 399, 358, 338)50000
读写锁1/2(1406, 1387, 1412, 1388, 1412)50000

小结:

读写锁QReadWriteLock 在指定线程写多个线程读,效率明显比多个线程同时读写好。

但是,都远低于互斥锁QMutex的执行效率。

http://www.lryc.cn/news/94776.html

相关文章:

  • 如何在 Windows 中免费合并 PDF 文件 [在线和离线]
  • 【LLM】金融大模型场景和大模型Lora微调实战
  • 途乐证券股市资讯-英伟达,又创历史新高!美股全线上涨
  • MySQL表聚合函数
  • JavaWeb 速通XML
  • redis浅析
  • 四种缓存的避坑总结
  • flutter开发实战-flutter二维码条形码扫一扫功能实现
  • 一篇文章了解Redis分布式锁
  • 记录第一次组装电脑遇到的坑
  • 右键pdf文件没有打印
  • 什么是CDN?CDN的原理和作用是什么?
  • 链路传播(Propagate)机制及使用场景
  • pytorch技巧总结1:学习率调整方法
  • 谈谈VPN是什么、类型、使用场景、工作原理
  • windows 下载安装Redis,并配置开机自启动
  • 2. CSS3的新特性
  • 从零开始训练神经网络
  • 连接区块链节点的 JavaScript 库 web3.js
  • js:scroll平滑滚动页面或元素到顶部或底部的方案汇总
  • 【Docker】Docker的部署含服务和应用、多租环境、Linux内核的详细介绍
  • C国演义 [第五章]
  • Proxy-Reflect使用详解
  • 【Linux后端服务器开发】Shell外壳——命令行解释器
  • 【无公网IP】在外Windows远程连接MongoDB数据库
  • mac python3 安装virtualenv
  • 网络安全(自学笔记)
  • SPSS方差分析
  • 【Linux】深入理解文件系统
  • 12.9 专用指令