Access to members of an object and atomicity

  • We know from the C # specification that read / write links are atomic. In a statement that refers to a member of an object, will the reference to the link be also atomically accessible? I think yes, because it is also a kind of implicit reference reading, which the compiler must provide atomicity when creating code for it.

  • In the same statement, access to an object to access its member will cause the reference to the objects to be saved so as not to collect garbage when a new instance is created by another thread?

  • So, if we refer to the members in the chain, will the link with the left objects be also saved, so this is not garbage collected by other threads?

Consider the following code:

static SomeClass sharedVar;

void someMethod()
{
    SomeClass someLocalVar = sharedVar.memberX.memberY.a;
    operations on someLocalVar...
}

, MSDN, # .. Microsoft, , , .

+3
2
  • , .
  • , .ldfld. CLR , . , , .
  • , , , . CLR , .
+3

GC. - , - . , , .

0

Source: https://habr.com/ru/post/1722792/


All Articles