开发者

thread safe containers in arrays

can anyone please explain, whether accessing an array by multiple threads, where each thread is working on a different element of the array.

so there are n elements, and n threads ==> the nth thread is working on the nth element of the array?

can any one be kind enough to explain if this is safe or not? and why or why not?

thanks p.s. interested in 开发者_高级运维the main languages c# or java, python but I would love any other inputs from esoteric language experts


This will, in most languages, be safe (provided you're only using any specific array element within one thread at a time) - however, it's not a good idea.

The main problem is one of false sharing. By writing to items on the array from multiple threads, you'll get very, very poor performance, since you're constantly swapping cache lines. It's a particularly bad idea in managed languages like C# and Java. For example, in C#, the CLR does bounds checking on the array. This causes any array access to access a variable (the length) stored just before the first element of the array, which constantly causes cache misses to occur. A great demo of this was done by Igor Ostrovsky at PDC - you can see the parallel performance is many times slower than handling it serially because of this issue.

It's a better idea to push the work into the threads as separate data variables, and "compose" your array after the fact.


As described this is thread safe, provided that each of the items in the array are different objects that don't interact with each other.

To be absolutely certain, don't add or remove items from the array while the threads are working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜