50,230 Members
3 added today
250,678 Resources
38 added today

All Devdex   All C#dex   Current Category
C#dex > Forums & Newsgroups > Newsgroups > microsoft.public.dotnet.languages.csharp Add this category to My Favorites

View Message Thread  (14 replies)

Results 1 - 10 of 15 Next Page »

what's the equivalent of ThreadLocal of Java for C# ? Add this thread to My Favorites
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

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.

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

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

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.

Results 1 - 10 of 15 Next Page »

 

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 Add this thread to My Favorites.



Credit Card Payment Control
Supports over 25 companies
Managed ASP.NET Solution
Direct Processor Support

ASP ArticlesThis category has been added to your weekly newsletter
ASP Web Sites
ADSI & WSH BooksThis category has been added to your weekly newsletter
FREE ComponentsThis category has been added to your weekly newsletter
ASP EventsThis category has been added to your weekly newsletter
ASP HeadlinesThis category has been added to your weekly newsletter

CSharp ArticlesThis category has been added to your weekly newsletter
C# Web SitesThis category has been added to your weekly newsletter

SQL ArticlesThis category has been added to your weekly newsletter
SQL Events
SQL HeadlinesThis category has been added to your weekly newsletter
SQL Jobs

Jobs in CaliforniaThis category has been added to your weekly newsletter

XML ArticlesThis category has been added to your weekly newsletter
XML BooksThis category has been added to your weekly newsletter
XML Web Sites
XML Tutorials

free asp host

"Alex Homer"This search has been added to your weekly newsletter

Edit My Favorites Edit Profile & Favorites

 

 

 

 

 




Developersdex Home | ASP | C# | SQL | VB | XML | Gurus
Add Your Link | Add Your Code | FAQ | Advertise | Link To Us | Contact Us |
Copyright © 2010 Developersdex™. All rights reserved.