zoukankan      html  css  js  c++  java
  • ThreadStatic

     private void foo()
            {
                var t 
    = new Thread(x =>
                {
                    
    //MyClass.MyProperty = 9;
                    MessageBox.Show(MyClass.My.I.ToString());
                });

                t.Start();
            }

            
    public class MyClass
            {
                [ThreadStatic]
                
    public static int MyProperty = 5;
                [ThreadStatic]
                
    public static MyClass My = new MyClass();

                
    public int I = 1;
            }

    threadstatic 类型标识的变量不需要给其赋值,不同的线程对它进行访问的时候,只在第一次会发生作用,

    在被第一次访问后,被重置为类型的默认值,

     第一次执行正常: MessageBox.Show(MyClass.My.I.ToString());
    第二次执行:异常了。
  • 相关阅读:
    cmake
    docker
    rust
    linux
    FPGA
    visual studio
    win+R
    word文档的导出(用freemarker模板导出)(桃)
    iconfont的引入方法
    jquery 日期插件
  • 原文地址:https://www.cnblogs.com/zbw911/p/2060126.html
Copyright © 2011-2022 走看看