If i write data using many threads into a property e.g: public decimal number;
will using volatile decimal allow me to read the decimal exception safe without using a lock?
If i write data using many threads into a property e.g: public decimal number;
will using volatile decimal allow me to read the decimal exception safe without using a lock?
A decimal
can not be marked volatile
. You will get a compiler error. The reason is that operations on this type can not be guaranteed to be atomic due to its size.
You can not assume that lock-free read will be safe. It may return a partially written value which may be invalid, and will definitely not be a desired value.