简体   繁体   English

.NET DynamicMethod线程安全吗?

[英].NET DynamicMethod Thread Safe?

If I write a DynamicMethod with an ILGenerator and the code that I output is thread safe would the resulting delegate be threadsafe? 如果我使用ILGenerator编写了DynamicMethod,并且我输出的代码是线程安全的,则生成的委托是否是线程安全的?

My concern is that the IL gets compiled the first time the method runs. 我担心的是, IL是在方法第一次运行时被编译的。 If that is true what happens if some other thread tries to run the delegate while it is compiling? 如果是这样,那么如果其他线程在编译时尝试运行委托,会发生什么情况呢?

It doesn't do any difference with a standard delegate. 与标准委托没有任何区别。 They are both present as MSIL somewhere and the JIT compile them when it wants. 它们都以MSIL形式出现在某个地方, JIT可以在需要时对其进行编译。

Once you have it in delegate form it is a body of code you could call. 一旦以委托形式获得它,便可以调用该代码体。 That's all you really need to know. 这就是您真正需要知道的。 The implementation of .NET you are running in (Rotor, Mono, Microsoft one) should shield you from any consideration about this, otherwise it's a bug in the implementation. 您正在其中运行的.NET的实现(Rotor,Mono,Microsoft一家)应该使您免受对此的任何考虑,否则,这是实现中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM