Can another thread see a partially created collection when using the collection initializer?

Introduce this C # code in some method:

SomeClass.SomeGlobalStaticDictionary = new Dictionary<int, string>()
{
    {0, "value"},
};

Say no one uses any explicit memory or lock barriers to access the dictionary.

If optimization is not performed, the global dictionary must be either zero (initial value) or a correctly constructed dictionary with one record.

A possible question is: can the effect of calling Add and assigning SomeGlobalStaticDictionary be reordered in such a way that some other thread sees an empty non-zero SomeGlobalStaticDictionary (or any other invalid partially constructed dictionary?)

Does the response change if SomeGlobalStaticDictionary is mutable?

After reading http://msdn.microsoft.com/en-us/magazine/jj863136.aspx (as well as its second part), I found out that theoretically just because one variable is assigned in the source code, other threads may seem different for many reasons. I looked at the IL code, but the question is whether the JIT compiler and / or the processor are allowed to not "clear" the effect of calling Add to other threads before assigning SomGlobalStaticDictionary.

+5
source share
2 answers

Let me start by saying that I do not know the answer to your question, but I can help you simplify it in essence:

unsafe class C
{
    static int x;  // Assumed to be initialized to zero
    static int *p; // Assumed to be initialized to null
    static void M()
    {
        int* t = &C.x;
        *t = 1;
        C.p = t;
    }
    ...

int , p , , t , x. , : , .

, # , C.p x x - .

, ; .

: ? p x . , x , p . , p , x - ? ?

+4

( ) , , Add ( ).

, :

class Test
{
    static List<int> StaticList = new List<int> { 1 };
    List<int> InstanceList = new List<int> { 2 };
}

IL:

.method private hidebysig specialname rtspecialname static 
        void  .cctor() cil managed
{
  // Code size       21 (0x15)
  .maxstack  2
  .locals init (class [mscorlib]System.Collections.Generic.List`1<int32> V_0)
  IL_0000:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
  IL_0005:  stloc.0
  IL_0006:  ldloc.0
  IL_0007:  ldc.i4.1
  IL_0008:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
  IL_000d:  nop
  IL_000e:  ldloc.0
  IL_000f:  stsfld     class [mscorlib]System.Collections.Generic.List`1<int32> Test::StaticList
  IL_0014:  ret
} // end of method Test::.cctor

IL:

.method public hidebysig specialname rtspecialname 
        instance void  .ctor() cil managed
{
  // Code size       29 (0x1d)
  .maxstack  3
  .locals init (class [mscorlib]System.Collections.Generic.List`1<int32> V_0)
  IL_0000:  ldarg.0
  IL_0001:  newobj     instance void class [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldc.i4.2
  IL_0009:  callvirt   instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
  IL_000e:  nop
  IL_000f:  ldloc.0
  IL_0010:  stfld      class [mscorlib]System.Collections.Generic.List`1<int32> Test::InstanceList
  IL_0015:  ldarg.0
  IL_0016:  call       instance void [mscorlib]System.Object::.ctor()
  IL_001b:  nop
  IL_001c:  ret
} // end of method Test::.ctor

, . , , , , , , Add. Add.

- , , , .

, , - /. , , , " " , .

- Microsoft .NET, "" (, # ECMA).

+6

All Articles