Are you familiar with [threadstatic]?

Source: Internet
Author: User

Reprinted: 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 of each Al. 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 ";
}< br> return _ Foo;
}< BR >}

Public static string goo {
get {
If (! _ State [stategooinitialized]) {
_ State [stategooinitialized] = true;
_ goo = "the goo string ";
}< br> return _ goo;
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.