c# - C# 中的私有(private)静态变量和线程安全

标签 c# variables static private

我的一位同事在多线程 C# 应用程序中编写了以下代码...

public class1
{
     private static partialClass var1 = new partialNonStaticClass();

     public static method1()
     {
        //do something with var1
     }
}

虽然 var1 是私有(private)的并且设置为非静态部分类,但它是静态的事实意味着它可以被所有线程共享。
此外,不对 var1 执行锁定。 因此,var1 不是线程安全的。

只是希望有人验证我是正确的。

最佳答案

, the fact that it is static means it could be shared by all threads.

这是真的。它可能可能被多个线程使用。

Also, no locking is performed on var1. Therefore, var1 is not threadsafe.

这可能是真的,也可能不是。如果 partialNonStaticClass 本身是线程安全的,则可能不需要锁定。

关于c# - C# 中的私有(private)静态变量和线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19144842/

相关文章:

c# - String.Replace 忽略大小写

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

ios - Swift:通过全局文件更改永久变量

c - 为什么 main() 不能在 C 中声明为静态的?

java - Android:静态变量和移动复杂数据

c++ - 在没有主体的情况下调用未实现的静态方法

c# - C#.Net 中的 Magento API : catalogProductRequestAttributes problem

c# 字典如何为单个键添加多个值?

c# - 控制流图,使用 Roslyn 进行内部/内部数据流分析,.NET 编译器平台

java - 如何使用 SQL 查询结果作为 Java 中变量的值?