4

I declare array like that private double[] array = new double[length]. Is it safe to update this array items in one thread and read in another thread? Will I have up to date value?

Note i do not enumerate array. I only access its items by index.

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

3 Answers3

4

Arrays are not thread safe, from MSDN:

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.

If you only update single items at a time I think that you will be safe though, but I would not trust it unless I found documentation that proves it.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • i do not modify array. i only modify items in array. i know that array is not thread safe. the question is - are array items volatile? – Oleg Vazhnev Jul 06 '12 at 08:00
  • 1
    See my answer - writes to double are not atomic, so volatile would not help you (and to my knowledge array items are not considered volatile nor can be made one -[volatile](http://msdn.microsoft.com/en-us/library/x13ttww7(v=vs.100).aspx) ). – Alexei Levenkov Jul 06 '12 at 08:06
2

Volatile does not guarantee freshness of a value. It prevents some optimizations, but does not guarantee thread synchronization.

Double is not guaranted to be updated atomically. So updating/reading arrays of doubles without synchronization will not be thread safe at all wit or without volatile as you may read partially written values.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • `as you may read partially written values` it's interesting. i was thinking that primitive assign is atomic operation... and also there are some more complex atomic operations in this class http://msdn.microsoft.com/ru-ru/library/system.threading.interlocked.aspx – Oleg Vazhnev Jul 06 '12 at 08:07
  • Normally when you say "update" `operator=` is assumed. If you use proper Interlocked ones it will be atomic. – Alexei Levenkov Jul 06 '12 at 08:12
  • Non-atomic updates of `double` is the key to answering this question - I think this should be the accepted answer. Interlocked actually works on array elements (I just tested, didn't think it would work). – Anders Abel Jul 06 '12 at 08:51
  • how to perform atomic update of double? Which `Interlocked` method should I use? Will that guarantee freshness? I.e. would `Interlocked` be "harder" method than volatile? – Oleg Vazhnev Jul 06 '12 at 10:11
  • [Interlocked.Exchange(Double,Double)](http://msdn.microsoft.com/ru-ru/library/3sh52ebc.aspx) will update double atomically `Interlocked.Exchange(ref arr[3], myValue)`. – Alexei Levenkov Jul 06 '12 at 17:01
0

No, they are not. You should design your locking system with semaphores or other methods to ensure thread safety. You can check producer/consumer problem.

Mehmet Ali Sert
  • 184
  • 2
  • 9