C ++ 14 shared_timed_mutex与VS C ++ 11互斥锁 [英] C++14 shared_timed_mutex VS C++11 mutex

查看:209
本文介绍了C ++ 14 shared_timed_mutex与VS C ++ 11互斥锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在8个线程(我有一个8核PC)之间有一个共享的哈希表,每个线程都在哈希表中读写.

I've an shared hash table between 8 threads (i've an 8 core PC), each thread reads and writes in the hash table.

在示例1中,我使用了经典的互斥锁,并且所有8个内核都处于100% 在示例2中,我使用了shared_timed_mutex,因为可以在竞争中进行读取访问,但是所有8个核心的访问量均为40%

in example 1 i used classic mutex and all 8 core are at 100% in example 2 i used shared_timed_mutex because read access can be in competition but all 8 core are at 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();

推荐答案

由于您的问题有些含糊,而且除您之外的任何人都无法重现该行为,所以我只能猜测.

Since your question is somewhat vague and the behavior is not reproducible by anyone aside from yourself, I can only guess.

我最好的猜测是:

shared_timed_mutex并不总是比mutex好.如果是这样,则不需要mutex.我们将摆脱mutex,将shared_timed_mutex重命名为mutex,然后过着幸福的生活.不幸的是,现实生活比这更复杂.

shared_timed_mutex is not always better than mutex. If it were, there would be no need for mutex. We would just get rid mutex, rename shared_timed_mutex to mutex, and live happily ever after. Unfortunately real life is more complicated than that.

有时mutex是高级工具.有时shared_timed_mutex是上乘的工具.

Sometimes mutex is the superior tool. Sometimes shared_timed_mutex is the superior tool.

例如:如果我们有8个线程争用互斥锁,并且每个线程有50%的概率需要读取或写入,并且读写任务要求将互斥锁保持大约相同的时间,则存在使用shared_timed_mutex类型几乎没有好处.

For example: If we have 8 threads contending for the mutex, and each thread has a 50% probability of needing to read or write, and the read and write tasks require holding the mutex approximately the same amount of time, then there is little gain in using a shared_timed_mutex type.

要理解这一点,请考虑同时请求shared_timed_mutex的所有8个线程的情况.如果编写者先得到它(50%的概率),然后所有其他7个线程都阻塞了(就像我们正在使用mutex一样).

To understand this, consider the scenario of all 8 threads requesting the shared_timed_mutex at the same time. In the case a writer gets it first (50% probability), then all 7 of the other threads block (same as if we were using a mutex).

在另外50%的时间里,读者首先获得了它.请求锁定的第二个线程有50/50的机会成为阅读者.如果是写者,则公平的实现将阻止其余的读者获得该锁.这种情况发生的时间为0.5 * 0.5 == 25%.因此,现在我们最多有75%的时间,一次只能运行一个线程:{W,block ...}和{R,W,block ...}.

In the other 50% of the time a reader gets it first. The second thread to request a lock has a 50/50 chance of being a reader. If it is a writer, a fair implementation will block the remaining readers from acquiring the lock. This happens 0.5 * 0.5 == 25% of the time. So now we're up to 75% of the time, only one thread can run at a time: {W, block...} and {R, W, block...}.

25%的时间我们得到{R,R,?...},其中至少两个线程可以同时运行.

25% of the time we get a {R, R, ?...}, where at least two threads can run at the same time.

好吧,至少25%的人一次获得两个甚至更多的线程值得吗?

Well, at least isn't 25% getting two and perhaps more threads running at once worth it?

要视情况而定.

mutexshared_timed_mutex更简单.更简单的代码导致更快的代码.

mutex is simpler than shared_timed_mutex. Simpler code leads to faster code.

shared_timed_mutex在阅读器的数量远远超过书写器的数量以及与书写器任务相比较长的和/或常见的阅读器任务时亮起.

shared_timed_mutex shines when the readers far outnumber the writers, and when the reader-task is long and/or common compared to the writer-task.

例如,如果您的数据库除了每年进行6次更新(并且需要花费1秒钟的更新时间)之外,都保持不变,那么读取shared_timed_mutex锁定在共享模式下的数据库非常有意义.可以轻松地以独特的模式每两个月将其锁定一次.

For example if you have a database that remains immutable except for being updated 6 times a year (and takes 1 second to update), it makes a lot of sense to read that database with a shared_timed_mutex locked in shared mode. One can easily afford to lock it for a second once every two months in unique mode.

但是,如果您尝试每秒更新该数据库,则情况将完全不同,并且shared_timed_mutex可能根本无法为您提供任何性能优势.在这种情况下,仅要求每个人(读取者和写入者)请求独占访问权限可能会比较便宜(因为逻辑更简单).

But if you are trying to update that database every second, this is a completely different situation, and shared_timed_mutex may not offer you any performance advantage at all. In this case it may be cheaper just to require everyone (reader and writer) to ask for exclusive access (because the logic is simpler).

这篇关于C ++ 14 shared_timed_mutex与VS C ++ 11互斥锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆