Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple threads accessing same multi-dimensional array [duplicate]

Possible Duplicate:
Are C# arrays thread safe?

I have been programming C# for 10 months now. I am now learning multi-threading and seems to be working nicely. I have multi-dimension array like

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

I have the threads calling methods that end up saving their outputs to different coordinates inside the array above. without any one thread writing to the same location as another thread.

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

My question is: I have read about using locks on objects like my array, do I have to worry at all about locks even though my threads might access to the test array at the same time but never write to the same coordinates(memory location)?

So far in my testing I have not had any issues, but if someone more seasoned than myself knows I could have issue then I'll look into use locks.

like image 216
john johnson Avatar asked Feb 09 '12 21:02

john johnson


People also ask

What will happen if multiple threads accessing the same resource?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

Can multiple threads access the same array?

The answer is no. Each array element has a region of memory reserved for it alone within the region attributed the overall array. Modifications of different elements therefore do not write to any of the same memory locations.

What are the advantages of using multiple threads over multiple processes?

On a multiprocessor system, multiple threads can concurrently run on multiple CPUs. Therefore, multithreaded programs can run much faster than on a uniprocessor system. They can also be faster than a program using multiple processes, because threads require fewer resources and generate less overhead.

Can multiple threads read the same value?

Even though the variable is not currently being written to, previous writes to the variable may not yet be visible to all threads. This means two threads can read the same value and get different results creating a race condition.


2 Answers

One thing to be careful about is if you are testing with a single core processor, everything might work fine, but as soon as you run on a multi-core processor, you run into problems with threads hitting a shared piece of memory at the same time.

Just to be safe, if the two threads are actually modifying your multi-array directly, you need to implement a locking system. This answer in StackOverflow has a good example of locking.

like image 25
urbadave Avatar answered Sep 27 '22 20:09

urbadave


If you can ensure that no two threads will ever attempt to read or write to the same element then you have no need to do any locking, and if you do add locking you will slow down your code.

You should however take the time to add comments as appropriate to explain that, (and possibly why) no threads ever access the same elements to avoid future problems that Jim Fell has mentioned in his answer.

Update: Many of the other posters are continuing to suggest that locking be used merely as a safeguard against mistakes by future developers. To that end, it really depends on your application. If performance really isn't much of an issue, then sure, go ahead and synchronize access to the elements.

However, most of the time in which multiple threads are accessing a single array the reason that multiple threads exist is to perform parallel processing on large amounts of data in which performance is a significant concern. If it wasn't much of a concern, then you could just use a single thread and be far more at ease about it not being messed up by others. In such high performance computing reliance on lock (in various forms) is, as a rule, minimized whenever possible. Synchronization through separation of data (meaning preventing them from ever reading/writing to the same memory locations) is far superior to using locks, when it is feasible.

like image 94
Servy Avatar answered Sep 27 '22 21:09

Servy