Array and secure thread access

If I have an array that can / will be accessed across multiple threads at any given time, what exactly causes it to be threadless and what steps will be taken to ensure that the array is thread safe in most situations?

I looked on the Internet many times and did not find almost any information on this issue, everything seems to be specific scenarios (for example, this array, to which these two streams refer both by thread and by and on). I would very much like someone to be able to answer the questions I posed above, or if someone can point to a good document explaining these points.

EDIT: After inspecting MSDN, I found the ArrayList class. When you use the synchronize method, it returns a thread-safe wrapper for the given list. When setting data in a list (ie List1 [someNumber] = anotherNumber;) does the wrapper automatically take care of blocking the list, or do you still need to block it?

+4
source share
4 answers

When two threads access the same resource (for example, not local copies, but actually the same copy of the same resource), several things can happen. In the most obvious scenario, if Thread # 1 accesses a resource and Thread # 2 modifies it in the middle of reading, unpredictable behavior may occur. Even with something as simple as an integer, you might get logical errors, so try to imagine the horrors that might arise from the misuse of something more complex, such as a database access class that is declared static.

The classic way to solve this problem is to block sensitive resources, so only one thread can use it at a time. So in the example above, Thread # 1 will request a resource lock and get it, then go in to read what you need to read. Topic number 2 will come in the middle of reading and request a resource lock, but will be refused and will be asked to wait, because Thread # 1 uses it. When Thread # 1 ends, it releases the lock, and it is OK for Thread # 2 to continue.

There are other situations, but this illustrates one of the most basic problems and solutions. In C # you can:

1) Use specific .NET objects that are managed as wireframe locked (e.g. Scorpion-Prince for SynchronizedCollection )

2) Use [MethodImpl (MethodImplOptions.Synchronized)] to indicate that a particular method that does something dangerous should be used only one thread at a time

3) Use a lock statement to isolate certain lines of code that do something potentially dangerous

Which approach best really depends on your situation.

+6
source

If I have an array that can / will be accessed by multiple threads at any given time, what exactly causes it to be threadless, and what steps will be taken to ensure that the array is thread safe in most situations?

In general terms, the fact that the array is not thread safe is that two or more threads can modify the contents of the array if you do not synchronize access to it.

Generally speaking, for example, suppose you have thread 1 that does this job:

for (int i = 0; i < array.Length; i++) { array[i] = "Hello"; } 

And thread 2 does the job (in the same shared array)

 for (int i = 0; i < array.Length; i++) { array[i] = "Goodbye"; } 

Nothing synchronizes the streams, so your results will depend on which stream wins the race first. It can be "Hello" or "Goodbye" in some random order, but it will always be at least "Hello" or "Goodbye".

The actual string entry "Hello" or "Goodbye" is guaranteed by the CLR to be atomic. That is, writing a value of "Hello" cannot be interrupted by a thread attempting to write "Goodbye." One must happen before or after the other, never in between.

So, you need to create some kind of synchronization mechanism to prevent switching arrays to each other. You can accomplish this using the lock statement in C #.

+4
source

C # 3.0 and above provide a common collection class called SynchronizedCollection , which "provides a thread-safe collection that contains objects of the type specified in the general parameter as elements."

+2
source

Array is thread safe if it is called public and static keywords - the moment is not guaranteed - because System.Array implements the ICollection interface, which defines some synchronization method to support the synchronization mechanism.

However, encoding for enumeration through an array element is unsafe; the developer must implement the lock statement to make sure that there are no changes during the enumeration of the array.

Example:

 Array arrThreadSafe = new string[] {"We", "are", "safe"}; lock(arrThreadSafe.SyncRoot) { foreach (string item in arrThreadSafe) { Console.WriteLine(item); } } 
0
source

All Articles