what's the equivalent of ThreadLocal of Java for C# ?

U

UFO

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...
 
A

Alberto Poblacion

UFO said:
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;
 
U

UFO

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.
 
T

Tom Shelton

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
 
U

UFO

again, please give me a code example. it's a too important feature that there
is no example of how to use it.
please.
 
U

UFO

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.
 
U

UFO

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 .
 
U

UFO

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.
 
U

UFO

about the dictionary example, i don't get what is has to do with concurrency
and threads.

about static members, i still think that its name is misleading, since
static means that it can always be accessible and that there is only one
instance of it through the whole running time of the program.however,
'static' is also a misleading keyword on Java, since there you have a static
class, which is a very weird thing to have as a name for an inner class.on C#
, static clsss means it's like a Singleton , which is a little more obvious.

about the ThreadLocal (or static thread member ) , will it be garbage
collected as soon as the thread finishes its running? if so, will an inner
function of the thread (if i choose to extend the thread) be able to reach
such a member? or maybe threads cannot be extended (derived) just because of
this?

about a code example, you can copy and paste the one i gave, and change what
is needed to be changed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top