态度决定高度、企图决定版图、格局决定结局

导航

Are you familiar with [ThreadStatic]?

转载: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;
            }
        }

posted on 2008-01-22 14:42  flyingchen  阅读(624)  评论(1编辑  收藏  举报