【发布时间】:2015-12-29 21:19:06
【问题描述】:
我有一个 8 个线程之间的共享哈希表(我有一个 8 核 PC),每个线程在哈希表中读取和写入。
在示例 1 中,我使用了经典的互斥锁,所有 8 个核心都处于 100% 在示例 2 中,我使用了 shared_timed_mutex,因为读取访问可能存在竞争,但所有 8 个内核都处于 40%
问题出在哪里?
example 1:
mutex mutex_hash;
-- thread --
mutex_hash.lock();
//read
mutex_hash.unlock();
..
mutex_hash.lock();
//write
mutex_hash.unlock();
=============================
example 2:
shared_timed_mutex mutex_hash;
-- thread --
mutex_hash.lock_shared();
//read
mutex_hash.unlock_shared();
..
mutex_hash.lock();
//write
mutex_hash.unlock();
【问题讨论】:
-
如果你的例子更完整,那真的很有帮助。
标签: c++ multithreading mutex c++14