当前位置:网站首页>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();
}
};
更多参考
边栏推荐
猜你喜欢

【记一个kaggle划水比赛】PetFinder.my - Pawpularity Contest 宠物预测

Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇

什么是私有云?您应该知道的 6 个优势

大手笔!两所“双一流”大学,获75亿元重点支持!

unity对象池(学习)

Homework 7.29 correlation function directory and file attributes related functions

奇异值分解(SVD)原理与在降维中的应用(附带例题讲解)(纯理论)

维护数千规模MySQL实例,数据库灾备体系构建指南

Mysql索引结构

微信视频号视频如何下载提取?视频号直播回放如何下载?方法很简单!
随机推荐
dolphinscheduler simple task definition and complex cross-node parameter transfer
无人艇轨迹跟踪的预设性能抗扰控制研究
什么是私有云?您应该知道的 6 个优势
北上广线下活动丨年底最不可错过的技术聚会都齐了
Homework 7.29 correlation function directory and file attributes related functions
【Kaggle:UW-Madison GI Tract Image Segmentation】肠胃分割比赛:赛后复盘+数据再理解
关于香港高防IP需要关注的几个问题
Scala基础:数组(Array)、映射(Map)、元组(Tuple)、集合(List)
监控界的最强王者,没有之一!
概率论得学习整理--番外3:二项式定理和 二项式系数
Rust 从入门到精通02-安装
概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率
Greenplum 6.0有哪些不可错过的硬核升级与应用?
Lake storehouse which electricity (2) of the project: project using technology and version and the environment
作业7.29 目录相关函数和文件属性相关函数
ModelCoder状态机:对柴油机工况判断策略进行建模
手慢无!阿里亿级流量高并发系统设计核心原理全彩笔记现实开源
MySQL查询性能优化
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
【记一个kaggle划水比赛】PetFinder.my - Pawpularity Contest 宠物预测