Two threads. The first furiously reading elements from the array. The second is equally furious in updating elements by reading them and incrementing them by an arbitrary amount.
Is this safe? can anything go wrong in this situation? I don't mind that the reading thread reads an 'old' value while the updating thread is still in the process of updating. I just want to make sure the reader doesn't ever read a number that was not written and also that an exception cannot occur.
An int update is atomic on all cpu architectures that can execute managed code. In other words, you won't read a value that has just a single byte modified by the writing thread. Value type values larger than 32-bit, like long and double are not guaranteed atomic. Object references are also always atomic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With