
View Message Thread (14 replies)
| what's the equivalent of ThreadLocal of Java for C# ? |
 |
From: UFO Date Posted: 5/14/2010 4:23:00 AM
hello,
i would like to know how to use a similar feature that exist on Java, on my
C# program.
basically , the ThreadLocal allows you to decare&use a variable to be set
per thread, without the need to assign it from outside the thread itself.it
means that whoever uses the code doesn't even know that each thread he
creates , when it uses the class, it gets a new variable just for this thread.
for example, if i want to have a threadID , so that each new thread will
have a new number (from 0 , ascending) , i could use the next code on Java:
// Assigns unique contiguous ids to threads.
public class ThreadID
{
// The next thread ID to be assigned
private static AtomicInteger nextID =new AtomicInteger(0);
// My thread-local ID.
private static ThreadLocalID threadID =new ThreadLocalID();
// return unique ID for this thread
public static int get()
{
return threadID.get();
}
// Reset this thread's ID.the parameter is the index new ID
public static void set(int index)
{
threadID.set(index);
}
// Assign new IDs from zero.
public static void reset()
{
nextID.set(0);
}
public static int getNumberOfRegisteredThreads()
{
return nextID.get();
}
private static class ThreadLocalID extends threadlocal<integer>
{
protected Integer initialValue()
{
return nextID.getAndIncrement();
}
}
}
so,for the first thread that uses this class by calling the 'get' function,
it will always (for all the next times that it calls this function) return 0.
for the second thread, it will always return 1 , and so on...

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: Alberto Poblacion Date Posted: 5/14/2010 5:30:00 AM
"UFO" < UFO@discussions.microsoft.com> wrote in message
news:60EF11FC-8CBF-4726-812A-F3B832CB398A@microsoft.com...
> i would like to know how to use a similar feature that exist on Java, on
> my
> C# program.
> basically , the ThreadLocal allows you to decare&use a variable to be set
> per thread, without the need to assign it from outside the thread itself.
You can apply a ThreadStaticAttribute to a static variable:
[ThreadStatic]
static int value;

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: UFO Date Posted: 5/14/2010 3:26:00 PM
to which variable ? the atomicInteger? if so,what about having a variable per
thread? if not, why should it be static?
can you please give me an example? maybe a way to do the same as what i've
written?
it's just that i've searched the internet for any example of how to do such
a thing and i didn't find even one.

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: Tom Shelton Date Posted: 5/14/2010 3:34:00 PM
UFO explained :
> to which variable ? the atomicInteger? if so,what about having a variable per
> thread? if not, why should it be static?
> can you please give me an example? maybe a way to do the same as what i've
> written?
> it's just that i've searched the internet for any example of how to do such
> a thing and i didn't find even one.
It sounds as if your talking about TLS - thread local storage. Here is
an article that covers a bit about this on msdn:
http://msdn.microsoft.com/en-us/library/6sby1byh.aspx
It talks about thread relative static fields - which uses the
ThreadStaticAttribute and about dynamic tls using the various Thread
related tls functions (Thread.AllocateNamedDataSlot, etc).
HTH
--
Tom Shelton

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: UFO Date Posted: 5/15/2010 4:48:00 PM
again, please give me a code example. it's a too important feature that there
is no example of how to use it.
please.

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: UFO Date Posted: 6/9/2010 3:00:00 PM

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: Peter Duniho Date Posted: 6/9/2010 10:36:00 PM

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: UFO Date Posted: 6/13/2010 3:01:00 AM
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 .

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: Peter Duniho Date Posted: 6/13/2010 6:19:00 PM
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

| Re: what's the equivalent of ThreadLocal of Java for C# ? |
|
From: UFO Date Posted: 6/14/2010 5:15:00 AM
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.

Would you like to track this thread?
By adding this News Thread to your Favorites Area you can refer to it later with just a click of the mouse.
Also you can optionally be notified instantly whenever there are any replies
posted to this Thread. To add this Thread to your Favorites Area just click on the red arrow next to the subject of the thread above
.
|