当前位置:网站首页>Hand tearing read-write lock performance test
Hand tearing read-write lock performance test
2022-07-30 12:53:00 【Jun Meng Ru Yan Brian】
测试代码
基本思路:
先用CountDownLatch (详见下节,Concurrency appliance) Synchronized child thread creation,然后,Press the timer once,开始测试.
再用另一个CountDownLatch Synchronized child threads to complete tasks,Finally press the timer again.
配置:
1个写线程,7个读线程.同时,Internal control read and write times,In order to easily reflect the performance of the read-write lock and the performance of the mutex lock,You can directly adjust the limit times of the inner loop.
粗略的结果:
When the number of writes is much less than the number of reads,The performance of read-write locks is higher than that of mutex locks.
When the number of writes is about the same as the number of reads,Mutex locks perform slightly better than read-write locks.
But after turning on code optimization,Only the number of writers is about two orders of magnitude less than the number of readers,The performance of read-write locks has only begun to be reflected.
CountDownLatch startLatch(1);
CountDownLatch endLatch(8);
ReadWriteLock mtx;
int g_count = 0;
void write_something() {
startLatch.wait();
int count = 1000; // thread_local
do {
WriterLockGuard lock(::mtx);
g_count++;
} while (--count);
endLatch.down();
}
void read_something() {
startLatch.wait();
int count = 100000; // thread_local
do {
#ifndef TEST_RWLOCK
WriterLockGuard lock(::mtx);
#else
ReaderLockGuard lock(::mtx);
#endif
} while (--count);
endLatch.down();
}
int main()
{
using namespace std::chrono;
std::vector<std::thread> threads;
for (int i = 0; i != 7; ++i) {
threads.push_back(thread(read_something));
}
for (int i = 0; i != 1; ++i) {
threads.push_back(thread(write_something));
}
startLatch.down();
auto start = std::chrono::system_clock::now();
endLatch.wait();
auto end = std::chrono::system_clock::now();
cout << (g_count == 1000) << endl;
cout << duration_cast<milliseconds>(end - start).count() << " ms" << endl;
for (auto &th : threads) {
th.join();
}
return 0;
}
Read-write lock implementation code
互斥锁 + Condition variables implement read-write locks
class ReadWriteLock {
std::atomic<int> nwait_;
std::condition_variable isNotWriter_;
std::mutex mtx_;
public:
void reader_lock() {
unique_lock<std::mutex> localGuard(mtx_);
while (nwait_ < 0) {
isNotWriter_.wait(localGuard);
}
nwait_++;
}
void reader_unlock() {
lock_guard<std::mutex> localGuard(mtx_);
if (--nwait_ == 0) isNotWriter_.notify_one();
}
void writer_lock() {
unique_lock<std::mutex> localGuard(mtx_);
while (nwait_ != 0) {
isNotWriter_.wait(localGuard);
}
--nwait_;
assert(nwait_ == -1);
}
void writer_unlock() {
lock_guard<std::mutex> localGuard(mtx_);
++nwait_;
assert(nwait_ == 0);
isNotWriter_.notify_all();
}
};
Double mutexes implement read-write locks
class ReadWriteLock {
int nwait_;
std::mutex rmtx_;
std::mutex wmtx_;
public:
ReadWriteLock() :nwait_(0) {
}
void reader_lock() {
lock_guard<std::mutex> localGuard(rmtx_);
++nwait_;
if (nwait_ == 1) wmtx_.lock();
}
void reader_unlock() {
lock_guard<std::mutex> localGuard(rmtx_);
--nwait_;
if (nwait_ == 0) wmtx_.unlock();
}
void writer_lock() {
wmtx_.lock();
}
void writer_unlock() {
wmtx_.unlock();
}
};
Concurrency appliance
CountDownLatch
javaCommon synchronization tools, It is usually used for sub-threads to aggregate to the main thread after all tasks are completed
class CountDownLatch {
size_t count_ = 0;
std::mutex mtx_;
std::condition_variable cond_;
public:
CountDownLatch(size_t count):count_(count) {
assert(count >= 1);
}
void down() {
unique_lock<std::mutex> localGurad(mtx_);
if (--count_ == 0) cond_.notify_all();
}
void wait() {
unique_lock<std::mutex> localGurad(mtx_);
while (count_ != 0) cond_.wait(localGurad);
}
};
LocalGuard
Some clever useRALLMechanism for thread synchronization tools
读写锁相关
WriterLockGuard:
class WriterLockGuard {
ReadWriteLock &lock_;
public:
WriterLockGuard(ReadWriteLock& lock) :lock_(lock) {
lock_.writer_lock();
}
~WriterLockGuard() {
lock_.writer_unlock();
}
};
ReaderLockGuard:
class ReaderLockGuard {
ReadWriteLock &lock_;
public:
ReaderLockGuard(ReadWriteLock& lock) :lock_(lock){
lock_.reader_lock();
}
~ReaderLockGuard() {
lock_.reader_unlock();
}
};
更多参考
边栏推荐
猜你喜欢

13-GuliMall Basics Summary

Win11打不开exe应用程序怎么办?Win11无法打开exe程序解决方法

AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...

JD.com was brutally killed by middleware on two sides. After 30 days of learning this middleware booklet, it advanced to Ali.

如何把Excel表格显示到邮件正文里?

为什么说Prometheus是足以取代Zabbix的监控神器?

打破原则引入SQL,MongoDB到底想要干啥???
![[BJDCTF2020]Cookie is so stable-1|SSTI injection](/img/48/34955bbe3460ef09a5b8213c7cc161.png)
[BJDCTF2020]Cookie is so stable-1|SSTI injection

概率论的学习整理1: 集合和事件

京东二面痛遭中间件虐杀,30天学透这套中间件小册,挺进阿里
随机推荐
来n遍剑指--04. 二维数组中的查找
js 构造函数 return 非空对象,其实例化的对象在原型上的差异
什么是驱动程序签名,驱动程序如何获取数字签名?
dolphinscheduler添加hana支持
概率论的学习整理--番外1:可重复且无次序的计数公式C(n+k-1,k) 的例题 : 同时丢3个骰子,会有多少种情况?答案不是216而是56!
Mysql batch insert transaction unique key repeated processing
[ASP.NET Core] Dependency Injection for Option Classes
概率论的学习整理--番外2:和二项式,组合相关的杨辉三角
开源出来的fuse版pfs文件系统主要就是解决缓存问题吧。nfs挂载参数带sync规避缓存问题是不是
dolphinscheduler单机化改造
从“校园贷”到“直播带货”,追风少年罗敏一直行走在风口浪尖
概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率
概率论得学习整理--番外3:二项式定理和 二项式系数
常见的云计算安全问题以及如何解决
Unity Beginner 6 - Simple UI production (blood bar production) and audio addition and NPC dialogue bubbles (2d)
Homework 7.29 correlation function directory and file attributes related functions
即时通讯-改变社交与工作状态的新型软件
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
崩了,该来的终究躲不掉
电脑奔溃的时候,到底发生了什么?