Are you familiar with [ThreadStatic]?

來源:互聯網
上載者:User

轉載:http://blogs.msdn.com/jfoscoding/archive/2006/07/18/670497.aspx

 

If you're stuffing anything in thread local storage, you might be interested in the performance comparison between Thread.SetData and [ThreadStatic].

ThreadStatic is super-cool, if you have a static variable, you can make it static "per-thread" by placing the attribute on top.  This is one easy way to get around the thread-safety issues of working with statics - as they are per-thread, you do not have to take a lock when updating them.

[ThreadStatic]
private static string Foo;

Now the thing that can trip you up is initialization. 

[ThreadStatic]
private static string Foo = "the foo string";

The ThreadStatic is initialized in the static constructor - which only executes once.  So only the very first thread is assigned "the foo string" when the static constructor executes. When accessed in all subsequent threads, Foo is left at the uninitalized null value.

The best way to work around this is to use a property to access the Foo prop.

[ThreadStatic]
private static string _foo;

public static string Foo {
   get {
     if (_foo == null) {
         _foo = "the foo string";
     }
     return _foo;
   }
}

If you have something that can be legitimately set to null, you can always use a thread-static boolean "_fooInitialized".

[ThreadStatic]
private static string _foo;

[ThreadStatic]
private static bool _fooInitialized;

public static string Foo {
   get {
     if (!_fooInitialized) {
         _fooInitialized = true;
         _foo = "the foo string";
     }
     return _foo;
   }
}

And if you have a lot of booleans, you may want to save space; believe it or not a boolean hogs precious bits (for extra credit try printing out Marshal.SizeOf(typeof(bool)). 

You save on space by lumping them all into one bitvector... 

        [ThreadStatic]
        private static string _foo;

        [ThreadStatic]
        private static string _goo;

        [ThreadStatic]
        private static BitVector32 _state = new BitVector32();
 

        private const int stateFooInitialized = 0x0001;
        private const int stateGooInitialized = 0x0002;
        // ... 0x0004, 0x0008 ...

        public static string Foo {
            get {
                if (!_state[stateFooInitialized]) {
                    _state[stateFooInitialized] = true;
                    _foo = "the foo string";
                }
                return _foo;
            }
        }

        public static string Goo {
            get {
                if (!_state[stateGooInitialized]) {
                    _state[stateGooInitialized] = true;
                    _goo = "the goo string";
                }
                return _goo;
            }
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.