静态初始化器和线程同步(.NET)

时间:2022-10-07 21:02:43

Static initializers are supposed to be executed once before the first reference to the class. It means that every time a class is accessed, a check should be performed whether the static initializers for the class are executed.
It seems that in multithreaded environment classes with non-trivial static initializers can be a source of contention because of synchronization necessary when the class is accessed by multiple threads.
My question is what is the best way to minimize the impact of such implicit locks on the class definitions introduced by static initializers?

静态初始化器应该在第一次引用类之前执行一次。这意味着每次访问类时,都应该检查是否执行了类的静态初始化程序。似乎在具有非平凡静态初始值设定项的多线程环境类中,由于多个线程访问类时需要同步,因此可能成为争用源。我的问题是什么是最小化这种隐式锁对静态初始化器引入的类定义的影响的最佳方法?

4 个解决方案

#1


The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

静态构造函数的执行由应用程序域中发生的以下第一个事件触发:

  • An instance of the class is created.
  • 创建了一个类的实例。

  • Any of the static members of the class are referenced
  • 引用该类的任何静态成员

It should be the responsibility of the class loader to handle concurrency issues when calling static constructors.

调用静态构造函数时,应该负责类加载器处理并发问题。

#2


I don't think there's much contention. The initializers are run when the class is loaded. If the class is already loaded then no checks have to occur.

我不认为有太多的争论。初始化程序在加载类时运行。如果已经加载了类,则不必进行检查。

#3


The specs says "executed at an implementation-dependent time prior to the first use of a static field of that class.".

规范说“在首次使用该类的静态字段之前,在与实现相关的时间执行”。

So, one can assume(or hope if you're pessimistic) that the implementation have figured out that they need to care about thread synchronization.

因此,人们可以假设(或者希望你是否悲观)实现已经发现他们需要关心线程同步。

#4


I believe this works as part of JIT compilation. Once the static initializer has been jitted and run, it no longer needs to run it and thus no synchronization problems with multiple threads. And of course the JIT engine will be thread-safe.

我相信这是JIT编译的一部分。一旦静态初始化程序被jitted并运行,它就不再需要运行它,因此多线程没有同步问题。当然,JIT引擎将是线程安全的。

Nick.

#1


The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

静态构造函数的执行由应用程序域中发生的以下第一个事件触发:

  • An instance of the class is created.
  • 创建了一个类的实例。

  • Any of the static members of the class are referenced
  • 引用该类的任何静态成员

It should be the responsibility of the class loader to handle concurrency issues when calling static constructors.

调用静态构造函数时,应该负责类加载器处理并发问题。

#2


I don't think there's much contention. The initializers are run when the class is loaded. If the class is already loaded then no checks have to occur.

我不认为有太多的争论。初始化程序在加载类时运行。如果已经加载了类,则不必进行检查。

#3


The specs says "executed at an implementation-dependent time prior to the first use of a static field of that class.".

规范说“在首次使用该类的静态字段之前,在与实现相关的时间执行”。

So, one can assume(or hope if you're pessimistic) that the implementation have figured out that they need to care about thread synchronization.

因此,人们可以假设(或者希望你是否悲观)实现已经发现他们需要关心线程同步。

#4


I believe this works as part of JIT compilation. Once the static initializer has been jitted and run, it no longer needs to run it and thus no synchronization problems with multiple threads. And of course the JIT engine will be thread-safe.

我相信这是JIT编译的一部分。一旦静态初始化程序被jitted并运行,它就不再需要运行它,因此多线程没有同步问题。当然,JIT引擎将是线程安全的。

Nick.