zoukankan      html  css  js  c++  java
  • Are C# delegates threadsafe?

    http://stackoverflow.com/questions/6349125/are-c-sharp-delegates-thread-safe

    Regarding the invocation of the delegate the answer is yes.

    Invoking a delegate is thread-safe because delegates are immutable. However, you must make sure that a delegate exists first. This check may require some synchronization mechanisms depending on the level of safety desired.

    For example, the following could throw a NullReferenceException if SomeDelegate were set to null by another thread between the null check and the invocation.

    if(SomeDelegate!=null){SomeDelegate();}

    The following is a little more safe. Here we are exploiting the fact that delegates are immutable. Even if another thread modifies SomeDelegate the code is harded to prevent that peskyNullReferenceException.

    Action local =SomeDelegate;if(local !=null){
      local();}

    However, this might result in the delegate never being executed if SomeDelegate was assigned a non-null value in another thread. This has to do with a subtle memory barrier problem. The following is the safest method.

    Action local =Interlocked.CompareExchange(refSomeDelegate,null,null);if(local !=null){
      local();}

    Regarding the execution of the method referenced by the delegate the answer is no.

    You will have to provide your own thread-safety guarentees via the use of synchronization mechanisms. This is because the CLR does not automatically provide thread-safety guarentees for the execution of delegates. It might be the case that the method does not require any further synchronization to make it safe especially if it never access shared state. However, if the method reads or writes from a shared variable then you will have to consider how to guard against concurrent access from multiple threads.

  • 相关阅读:
    CSS之Position详解
    线性回归预测法linear regression
    置信区间
    asp.net MVC 中使用dataannotation验证Model
    决策树Decision Tree
    Net反射在项目中的应用
    C#并行编程并行任务
    一个特殊的产品价格制定法(市场决定价格)
    Json
    线性规划模型(线性优化模型)Linear programming
  • 原文地址:https://www.cnblogs.com/anorthwolf/p/3124281.html
Copyright © 2011-2022 走看看