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.
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.
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.
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.
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.