Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C# arrays thread safe?

In particular

  1. Create a function to take an array and an index as parameters.
  2. Create a n element array.
  3. Create a n count loop.
  4. Inside the loop on a new thread assign a new instance of the object to the array using the indexer passed in.

I know how to manage the threads etc. I am interested in know if this is thread safe way of doing something.

 class Program {     // bogus object     class SomeObject     {         private int value1;         private int value2;          public SomeObject(int value1, int value2)         {             this.value1 = value1;             this.value2 = value2;         }     }      static void Main(string[] args)     {          var s = new SomeObject[10];         var threads = Environment.ProcessorCount - 1;         var stp = new SmartThreadPool(1000, threads, threads);         for (var i = 0; i < 10; i++)         {             stp.QueueWorkItem(CreateElement, s, i);         }      }      static void CreateElement(SomeObject[] s, int index)     {         s[index] = new SomeObject(index, 2);     } } 
like image 422
Gary Avatar asked Sep 22 '09 15:09

Gary


People also ask

Are C and C++ the same?

While both C and C++ may sound similar, their features and usage are different. C is a procedural programming language and does not support objects and classes. C++ is an enhanced version of C programming with object-oriented programming support.

What is meant by C?

noun plural c's, C's or Cs. the third letter and second consonant of the modern English alphabet. a speech sound represented by this letter, in English usually either a voiceless alveolar fricative, as in cigar, or a voiceless velar stop, as in case.

Is there a C+?

C+ (grade), an academic grade. C++, a programming language. C with Classes, predecessor to the C++ programming language. ANSI C, a programming language (as opposed to K&R C)

What is ment by C programming?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.


2 Answers

I believe that if each thread only works on a separate part of the array, all will be well. If you're going to share data (i. e. communicate it between threads) then you'll need some sort of memory barrier to avoid memory model issues.

I believe that if you spawn a bunch of threads, each of which populates its own section of the array, then wait for all of those threads to finish using Thread.Join, that that will do enough in terms of barriers for you to be safe. I don't have any supporting documentation for that at the moment, mind you ...

EDIT: Your sample code is safe. At no time are two threads accessing the same element - it's as if they each have separate variables. However, that doesn't tend to be useful on its own. At some point normally the threads will want to share state - one thread will want to read what another has written. Otherwise there's no point in them writing into a shared array instead of into their own private variables. That's the point at which you need to be careful - the coordination between threads.

like image 199
Jon Skeet Avatar answered Sep 19 '22 12:09

Jon Skeet


MSDN documentation on Arrays says:

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

This implementation does not provide a synchronized (thread safe) wrapper for an Array; however, .NET Framework classes based on Array provide their own synchronized version of the collection using the SyncRoot property.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

So no, they're not thread safe.

like image 31
Joren Avatar answered Sep 20 '22 12:09

Joren