views:

77

answers:

2

Hi, 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

+2  A: 

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.

John Knoeller
+2  A: 

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.

Reed Copsey
Thank you for your input Reed. I was just reading about cache trashing recently, but failed to connect the dots until you mentioned. I am betting that the requirements I am assigned will be much better served with threads, versus serially. I am limiting the num of threads to a very small amount as well. thank you very much
bushman
Yeah, if you're doing HTTP calls per thread, it probably doesn't matter...
Reed Copsey