1

Possible Duplicate:
Are C# arrays thread safe?

I have a program that is single-threaded at the moment, which basically puts lots of computed data into a multidimensional array (e.g. double[,] ; string[,] ).

Is it possible to assign segments of this array to different threads ? More precisely, if I make sure only one thread will write at a given coordinate, will there be some lock mechanism triggered ?

Community
  • 1
  • 1
BuZz
  • 16,318
  • 31
  • 86
  • 141

2 Answers2

3

In terms of concurrency problems, you will be fine as long as your threads do not read or write to the same portion of your array concurrently. You may see slowdowns because of "False Sharing" hazard, though, so you may want to be on the lookout for unexpected slow-downs when the number of threads increases.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

if I make sure only one thread will write at a given coordinate

Then you are safe. Assuming you don't resize the array etc.

If you are now using a for loop you can probably simply switch to Parallel.For(0, n, method)

H H
  • 263,252
  • 30
  • 330
  • 514