Parallel write to array

I have a huge data array where I need to read / write from / to a random place from different streams. Having one mutex will obviously kill performance. My idea is to have many mutexes , each of which is responsible for a specific range in the array. Thus, before writing, I can block the correct mutex based on the index in the array where I am going to write. Theoretically, this can reduce the race. But I wonder - maybe the best way?

+5
source share
1 answer

It sounds like a reasonable path.

There are a few things to consider, though:

  • You state that your idea is to have "many mutexes, each of which is responsible for a specific range in the array." You should probably consider access patterns to decide how to assign entries to mutexes. If streams will work with closed records, you can consider assigning records to mutexes using a different scheme, for example, a record index modulo the number of mutexes.

  • From experience, we note that the number of mutexes should be determined by the number of threads, and not by the size of the range. I wrote more about this in this question (this is an accepted answer, at the time of writing this).

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

+4
source

Source: https://habr.com/ru/post/1241347/


All Articles