简体   繁体   English

非静态类线程中的C#静态方法安全吗?

[英]Is my C# static method in a non static class thread safe?

I have a C# class that can be instantiated with different constructors, but it also contains some static helper methods which only work with their parameters and can be called from other classes as well. 我有一个C#类,可以用不同的构造函数实例化它,但是它还包含一些静态帮助器方法,这些方法只能使用其参数,也可以从其他类中调用。

Now I wonder whether concurrent calls from different threads to such a method is thread safe or not, or in other words, is it possible that variables inside the static method get changed by call2, while call1 is still in process? 现在,我想知道从不同线程到该方法的并发调用是否是线程安全的,或者换句话说,当call1仍在处理时,静态方法内的变量是否可能被call2更改?

public class AwesomeClass
{
  /* constructors and stuff */

  public static bool HelperMethod(object o1)
  {
    string someString = "";
    Func<object, string> doSomething = (o) => { 

        string someOtherString == null;
        if (someString.Length == 0)
        {
          /* time consuming process using o... frequently
             changes someString and someOtherString */
        }
        return someOtherString;
    };
    return doSomething(o1).Length > 0 && someString.Length < 10;
  }
}

If someString in the example could be changed by a thread2 calling HelperMethod while doSomething is still working for thread1, I'd be in trouble. 如果在doSomething仍适用于线程1的情况下,线程2可以通过调用HelperMethod的thread2更改示例中的someString ,那么我会遇到麻烦。

If your static method does not change any member variables, and does not call mutating methods on its parameters, it is re-entrant, and is, therefore, thread-safe. 如果您的静态方法未更改任何成员变量,并且未在其参数上调用变异方法,则它是可重入的,因此是线程安全的。

Static methods that perform mutating operations on their parameters may not be thread-safe when concurrent threads invoke the method with the same object as its parameter. 当并发线程使用与其参数相同的对象调用该方法时,对其参数执行变异操作的静态方法可能不是线程安全的。

For example, if your method mutates o1 through methods, properties, or public variables, the method would no longer be thread-safe. 例如,如果您的方法通过方法,属性或公共变量使o1突变,则该方法将不再是线程安全的。

HelperMethod is thread safe because it does not access any shared data. HelperMethod是线程安全的,因为它不访问任何共享数据。 someString is local variable and it will be allocated again every time the method is called. someString是局部变量,每次调用该方法时都会再次分配它。

Note that this answer is not affected by the fact that someString is part of the closure. 请注意,此答案不受someString是闭包的一部分这一事实的影响。 Separate closure object (which contains particular instance of someString ) will be allocated on stack with every call to the HelperMethod . 每次调用HelperMethod将在堆栈上分配一个单独的闭包对象(其中包含someString特定实例)。

The variables someString and someOtherString are local to the call, they are not shared between calls. 变量someStringsomeOtherString在调用中是本地的,在调用之间不共享。 This is just as if had a function call itself, modifying variables in the 2nd call would not modify the first. 就像有一个函数调用本身一样,在第二个调用中修改变量不会修改第一个调用。

The one thing that could be unsafe is time consuming process using o... , If the operations you are doing to o1 are not safe to be done by multiple threads at once you could run into issues. 可能不安全的一件事是time consuming process using o... ,如果您对o1进行的操作不安全, time consuming process using o...要由多个线程完成就可能会遇到问题。

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

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