Prev: MemberInfo derived classes
Next: IComparer
From: UFO on 9 Jun 2010 16:58 it seems that dot net 4 has the ThreadLocal class: http://msdn.microsoft.com/en-us/library/dd642243.aspx that's a very good thing. however,i wonder how one can use the same thing for dot net 3 . using other methods like this one: http://www.java2s.com/Tutorial/CSharp/0420__Thread/Usethreadlocalstorage.htm or this: http://msdn.microsoft.com/en-us/library/system.threading.thread.getnameddataslot.aspx could work, but it makes the inner information to be public for all to read and change , which is a bad thing for encapsulation.
From: Peter Duniho on 10 Jun 2010 00:34 UFO wrote: > it seems that dot net 4 has the ThreadLocal class: > http://msdn.microsoft.com/en-us/library/dd642243.aspx > that's a very good thing. > however,i wonder how one can use the same thing for dot net 3 . using other > methods like this one: > http://www.java2s.com/Tutorial/CSharp/0420__Thread/Usethreadlocalstorage.htm > or this: > http://msdn.microsoft.com/en-us/library/system.threading.thread.getnameddataslot.aspx > could work, but it makes the inner information to be public for all to read > and change , which is a bad thing for encapsulation. The [ThreadStatic] attribute has been present since .NET 1.0. The new ThreadLocal class provides a mechanism to accomplish the same thing for instance members, but there's always been a reasonably convenient way to accomplish thread-local storage. Pete
From: UFO on 13 Jun 2010 04:59 not sure if i understand you. i do not need a static variable. i need to have a variable that is per thread . each thread has its own instance of the variable , and once the thread is gone (whatever the reason is) , its variable is gone with it as well (only if there is no reference to it, of course) . i also do not want outer methods and threads to access one thread's variable (unless i use some mechanism to overcome this) . if you think that this is exactly what i need, please post a super short and simple example. i suggest having the example that gives each new thread a number (like an id ) that starts with the value 0 for the first thread, 1 for the second , and so on .
From: Peter Duniho on 13 Jun 2010 19:21 UFO wrote: > not sure if i understand you. i do not need a static variable. i need to have > a variable that is per thread . The two are not mutually exclusive. > each thread has its own instance of the > variable , and once the thread is gone (whatever the reason is) , its > variable is gone with it as well (only if there is no reference to it, of > course) . That's what [ThreadStatic] does. > i also do not want outer methods and threads to access one thread's variable > (unless i use some mechanism to overcome this) . Protecting a variable from specific methods is simply a matter of accessibility, as it is in any scenario, except of course that with thread-local storage, you get automatic protection from any code not executing in the thread with which the variable is associated. And of course protecting the variable from other threads is exactly what thread-local storage does, so that aspect is there by definition. > if you think that this is exactly what i need, please post a super short and > simple example. i suggest having the example that gives each new thread a > number (like an id ) that starts with the value 0 for the first thread, 1 for > the second , and so on . I never said anything about the existing feature being "exactly what you need". My point is that from a static thread-local variable, you can construct any other thread-local data storage you need. Static members come for free, that being exactly what [ThreadStatic] does, and instance variables can be implemented on top of a static variable (e.g. with a thread-local dictionary mapping instances to a value). It's great that we now have an even more convenient way to have thread-local instance variables in classes, but as I said before, it's not like it was impossible, or even all that inconvenient, to implement code that had thread-local instance-specific data storage in any previous version of .NET. Pete
From: UFO on 14 Jun 2010 07:12
i don't get it. why does it called 'static' ? also, does each instance garbage collected as soon as it is not referenced (per thread) ? and please, please give me an example of how it works. |