Questions tagged [thread-local]

Thread-local is a class from the Java API and the documentation defines it: "This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID)."

This is a class from the Java API and the documentation defines it this way:

This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID)."

What questions should have this tag?

  1. Questions that deal with the concept of thread-local directly.
  2. Questions dealing with design-patterns based on using this class.
  3. Questions which have issues while using this class.

Useful Links

  1. Official Documentation
  2. A short tutorial on Thread Local
  3. How to shoot yourself in foot with ThreadLocals Introduction to java ThreadLocal storage

Important questions

  1. When and how should I use a ThreadLocal variable?
  2. Performance of ThreadLocal variable
580 questions
31
votes
9 answers

Can you use thread local variables inside a class or structure

Like this. struct some_struct { // Other fields ..... __thread int tl; } I'm trying to do that but the compiler is giving me this error. ./cv.h:16:2: error: '__thread' is only allowed on variable declarations __thread int tl;
pythonic
  • 20,589
  • 43
  • 136
  • 219
30
votes
6 answers

What is the correct way to dispose elements held inside a ThreadLocal?

When you use a ThreadLocal and T implements IDisposable, how are you supposed to dispose of the members being held inside of the ThreadLocal? According to ILSpy, the Dispose() and Dispose(bool) methods of ThreadLocal are public void Dispose() { …
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
29
votes
1 answer

Multiple independent embedded Python Interpreters on multiple operating system threads invoked from C/C++ program

Embedding Python interpreter in a C/C++ application is well documented. What is the best approach to run multiple python interpreter on multiple operating system threads (i.e. one interpreter on one operating system thread within the same process)…
bhadra
  • 12,887
  • 10
  • 54
  • 47
29
votes
1 answer

When is a `thread_local` global variable initialized?

Consider the following example (lock guards on cout omitted for simplicity). #include #include #include using namespace std; struct C { C() { cout << "C constructor\n";} ~C() { cout << "C…
sbabbi
  • 11,070
  • 2
  • 29
  • 57
29
votes
5 answers

Java ThreadLocal static?

Setting a value in Thread Local: //Class A holds the static ThreadLocal variable. Class A{ public static ThreadLocal myThreadLocal = new ThreadLocal(); .... } //A Class B method sets value in A's static…
Jasper
  • 8,440
  • 31
  • 92
  • 133
28
votes
6 answers

Servlet 3 spec and ThreadLocal

As far as I know, Servlet 3 spec introduces asynchronous processing feature. Among other things, this will mean that the same thread can and will be reused for processing another, concurrent, HTTP request(s). This isn't revolutionary, at least for…
mindas
  • 26,463
  • 15
  • 97
  • 154
28
votes
1 answer

Is SLF4J thread-safe?

I might have a Dog class that has a single instance shared across multiple threads. I plan on using SLF4J for all logging: public class Dog { private Logger logger = LoggerFactory.getLogger(Dog.class); // ...etc. } Is my logger instance…
user1768830
28
votes
1 answer

Why did the language designers of Java preferred chaining over open addressing for most hash based structures except for some like ThreadLocal?

I know the difference between Open Addressing and Chaining for resolving hash collisions . Most of the basic hash based data structures like HashSet,HashMap in Java primarily use chaining technique. I read that ThreadLocal actually uses a probing…
Geek
  • 26,489
  • 43
  • 149
  • 227
27
votes
7 answers

How to identify and remove Threads/ThreadLocals initiated from our webapp in Java?

Whenever I stop or redeploy the webapp, I see lot of errors similar to, msg=The web application [] created a ThreadLocal with key of type [] (value []) and a value of type [] (value []) but failed to remove it when the web application was stopped.…
AndyT
  • 421
  • 6
  • 13
26
votes
4 answers

Are C++ exceptions sufficient to implement thread-local storage?

I was commenting on an answer that thread-local storage is nice and recalled another informative discussion about exceptions where I supposed The only special thing about the execution environment within the throw block is that the exception…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
25
votes
2 answers

ThreadStatic vs. ThreadLocal Performance: speedups or alternatives?

I recently read this post about poor performance of fields marked ThreadStatic - they're apparently 60x slower than normal field access. Does .NET 4's ThreadLocal< T > perform any better? Are there any alternatives that offer high performance…
Mark
  • 9,320
  • 6
  • 57
  • 70
24
votes
4 answers

How to improve performance of SimpleDateFormat wrapped in ThreadLocal?

This is on Java 7 (51) on RHEL with 24 cores We are noticing a rise in average response times of a java SimpleDateFormat wrapped in thread local as we increase the thread pool size. Is this expected? or, I am just doing something stupid ? Test…
23
votes
7 answers

Threadlocal remove?

When using a ThreadLocal should I always call remove() when I am done or when I do set the old value is replaced anyway so remove is redundant?
Jim
  • 18,826
  • 34
  • 135
  • 254
22
votes
5 answers

What is so bad with threadlocals

Everybody in Django world seems to hate threadlocals(http://code.djangoproject.com/ticket/4280, http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser). I read Armin's essay on…
agiliq
  • 7,518
  • 14
  • 54
  • 74
20
votes
8 answers

ThreadLocal Resource Leak and WeakReference

My limited understanding of ThreadLocal is that it has resource leak issues. I gather this problem can be remedied through proper use of WeakReferences with ThreadLocal (although I may have misunderstood this point.) I would simply like a pattern…
Julien Chastang
  • 17,592
  • 12
  • 63
  • 89
1
2
3
38 39