How does the ThreadStatic attribute work?

后端 未结 4 656
余生分开走
余生分开走 2020-12-04 07:47

How does [ThreadStatic] attribute work? I assumed that the compiler would emit some IL to stuff/retrieve the value in the TLS, but looking at a disassembly it d

4条回答
  •  旧巷少年郎
    2020-12-04 08:46

    The field marked with [ThreadStatic] are created on Thread Local Storage so every thread has it own copy of the field i.e the scope of the fields are local to the thread.

    TLS fields are access through gs/fs segments registers.These segments are used by the OS kernels to access thread-specific memory.The .net compiler do not emit any IL to stuff/retrieve the value in the TLS. It is done by the OS kernel.

提交回复
热议问题