1

In this PostSharp Multithreading example it states in Listing 6:

Any method that modifies the object should be annotated with the [WriteLock] custom attribute. Methods that read more than one field of the object should also be annotated with the [ReadLock] custom attribute. (It is useless to synchronize methods or property getters performing a single read access, because the operation is always consistent.)

What does it mean that it's useless to synchronize methods or property getters performing a single read access? Is that only true for simple primitives such as string, int, float, etc. or does it apply to structs as well? Or is it a misleading statement?

Todd Smith
  • 17,084
  • 11
  • 59
  • 78
  • this is a bit misleading - as I understand it, assuming reading is safe, single read is ok. If you have object the some of this values depend on his other values, when read more than 1 property - need to add the above attribute. But this is me ..... – Mzf Mar 31 '14 at 22:13
  • As I understand they're saying that their Object is not ThreadSafe, therefore you have to do the synchronization yourself with Reader and writer locks. – Maxim Mar 31 '14 at 22:52

1 Answers1

1

Most likely it talks about "operations on pointer size objects are atomic" - so if you make single read of reference OR accordingly sized struct you are guaranteed to have consistent value without locks.

Consistent means you can't get half pointer from old value of property and half from new value of a property. Note that it does not mean latest value - if you need latest value you still need synchronization of some sort.

For more detailed information What operations are atomic in C#?.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179