平行写入数组 [英] Parallel write to array

查看:160
本文介绍了平行写入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的数据,我需要从不同的线程读/写随机的地方。有一个 mutex 显然会杀死性能。我的想法是有许多互斥体每个负责数组中的特定范围。这样写前我可以锁定一个正确的 mutex 基于数组中的索引,我要写。在理论上它可以减少种族。但我不知道 - 也许有更好的方法?

I have a huge array of data where I need to read/write from/to random place from different threads. Having one mutex obviously will kill the performance. My idea is to have many mutexes each one is responsible for particular range in array. This way before writing I can lock a correct mutex based on index in array where I'm gonna to write. In theory it can reduce the race. But I wonder - maybe there's a better way?

推荐答案

这听起来像一个合理的方式去。

That sounds like a reasonable way to go.

但有许多事情需要考虑:

There are a number of things to consider, though:


  1. 你说你的想法是有许多互斥,每一个负责特定范围的数组。您应该考虑访问模式来决定如何为互斥体分配条目。如果线程倾向于在close-by条目上工作,您可以考虑使用不同的方案将条目分配给互斥体,例如条目索引对互斥体的数量进行模。

  1. You state that your idea is to have "many mutexes, each one is responsible for particular range in array". You should probably consider the access patterns to decide how to assign entries to mutexes. If threads will tend to work on close-by entries, you might consider assigning entries to mutexes using a different scheme, e.g., the entry index modulo the number of mutexes.

根据经验,注意互斥体的数量应该由线程数量决定,而不是范围的大小。我在这个问题(这是

From experience, note that the number of mutexes should be determined by the number of threads, not the range's size. I wrote on this more in this question (it is the accepted answer, at the time of writing this).

再次根据使用模式,您应该考虑使用读/写锁来避免不必要的序列化多个读者在同一条目上。 YMMV。

Again depending on the usage pattern, you should consider using read/write locks to avoid needless serialization for multiple readers on the same entry. YMMV.

这篇关于平行写入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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