Multiple threads accessing the same multidimensional array

Possible duplicate:
Are C # Array Streams Safe?

I have been programming C # for 10 months. Now I'm learning multithreading and it seems to work great. I have a multidimensional array like

string[,] test = new string[5, 13]; 

I have methods for calling threads that ultimately save their outputs in different coordinates inside the array above. without any single thread writing to the same place as another thread.

So, thread1 can write to test[1,10] , but no other thread will write to test[1,10]

My question is: I read about the use of locks on objects such as my array, do I need to worry about locks at all, although my threads can access the test array at the same time, but never write to the same coordinates (memory)?

So far in my testing, I have not had any problems, but if someone is more experienced than me, I could solve the problem, then I will consider using locks.

+7
source share
4 answers

If you can make sure that none of the two threads tries to read or write to the same element, then you do not need to do a lock, and if you add a lock, you will slow down your code.

However, you need to take the time to add comments to explain that (and possibly why) no thread ever accesses the same elements to avoid future issues that Jim Fell mentioned in his answer.

Update. Many other posters continue to suggest that blocking be used only as protection against errors by future developers. To this end, it really depends on your application. If performance is really not a big issue, then be sure to continue and synchronize access to items.

However, most of the time when multiple threads access the same array, the reason that multiple threads exist is to process large amounts of data in parallel, in which performance is a significant problem. If it didn’t cause much concern, you could just use one thread and be more relaxed if it were not mixed up by others. With such high efficiency, block-dependent computation (in various forms) tends to be minimized when possible. Synchronization through data sharing (which means they never read / write to the same memory locations) is far superior to using locks when possible.

+7
source

One thing to be careful about is that you are testing a single-core processor, everything can work fine, but as soon as you start the multi-core processor, you run into problems when threads hit the shared memory at the same time.

To be safe, if two threads actually modify your array with multiple arrays, you need to implement a locking system. Stack Overflow

+1
source

You must use lock statements to ensure that your data object is thread safe. It may work now, but later (especially when / if the code is updated) you (or your replacement) may encounter elusive race errors or data corruption. Check out this article on the MSDN website . It has a good example of how to use lock statements.

0
source

Locks are intended only to prevent simultaneous access. Therefore, if you guarantee it in another way, you do not need locks.

PS I suppose you assigned each thread to each element of the array, right?

-one
source

All Articles