gpt4 book ai didi

c++ - boost::shared_mutex 在 Linux 上比粗略的 std::mutex 慢

转载 作者:搜寻专家 更新时间:2023-10-31 00:30:22 24 4
gpt4 key购买 nike

我有一个 std::unordered_map它受到来自多个线程的非常繁重的读取工作负载的影响。我可以使用 std::mutex用于同步,但由于并发读取应该没问题,我想使用 boost::shared_mutex反而。为了测试性能改进,我首先用一堆值预填充一个映射,然后让一堆线程运行 read test。 :

for (int i = 0; i < iters; ++i) map.count(random_uint(0, key_max));

我为我的 coarse-lock implementation 运行这个其中 countstd::lock_guard<std::mutex> 保护和我的 shared-lock implementation它受 boost::shared_lock<boost::shared_mutex> 保护的地方.

在我的带有 GCC 6.1.1 的 Arch Linux x86_64 系统上 boost::shared_lock版本总是较慢!在我 friend 的 Windows 10 系统上使用 MSVC 2013,boost::shared_lock 总是更快!完整的、可编译的代码在 github 上:https://github.com/silverhammermba/sanity

编辑

这似乎是一个特定于平台的问题。往上看。如果其他人可以构建和运行此代码并报告他们看到的是正输出(shared_lock 更快)还是负输出(当然互斥锁更快)以及您使用的平台,我将不胜感激。

最佳答案

原来 boost::shared_mutex"suboptimal"在 Linux 上。

The current (as of boost 1.59) implementation of boost::shared_mutex for 'pthread' is pretty suboptimal as it's using a heavyweight mutex to guard the internal mutex state... [when access concurrency is high] the shared mutex is effectively exclusive.

万岁,它偷走了我生命中的许多时光。

关于c++ - boost::shared_mutex 在 Linux 上比粗略的 std::mutex 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37303342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com