none
Static fields and thread safety RRS feed

  • Question

  • Hi,

    I'm wondering if static (Shared in VB) fields are synchronized by default, i.e., you don't need any synchronization code to have an assignment to the field visible immediately by all threads in the AppDomain that may read it. That would typically need that static fields were not ever cached in registers and processor caches.

    Thank you very much.

    Friday, October 19, 2012 11:57 AM

Answers

  • No, thread synchronized access is not provided when accessing static fields.

    "Visibility of a change" is entirely a different thing from synchronized access because the latter is so much subtle (like for example, partial updates of 64-bit locations in 32-bit memory structures). By visibility, you mostly mean "volatile" fields or "interlocked" statements.

    All the CLR ensures with respect to "static"ism is just this: The static constructor for a type is executed only once even when you have multiple threads in the AppDomain.

    Friday, October 19, 2012 3:40 PM