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
12
votes
1 answer

Static ThreadLocal variable in a WebApp - Are there any Security/Performance issues?

I am researching and experimenting with a ThreadLocal variable in my Java Web Application. I am using the ThreadLocal variable to store a username (collected from the session) before a request, and then removing it after the request. I have done…
javagruffian
  • 607
  • 2
  • 7
  • 13
12
votes
3 answers

How to clean up Java ThreadLocals in accordance with Sonar?

A Sonar rule available since Aug 21, 2019 (squid:S5164 / RSPEC-5164) mandates to clean up "ThreadLocal" variables when no longer used. So, let's take the following class (JDK6 compatible): public class ThreadLocalExample { private static final…
Robert Hume
  • 1,129
  • 2
  • 14
  • 25
11
votes
3 answers

Valgrind errors with boost::thread_specific_ptr on GCC 8.3 + Linux

Ubuntu 19 running inside Docker GCC 8.3 Boost 1.69 Valgrind 3.14.0 When the application is shutting down Valgrind reports these 3 issues: ==70== Mismatched free() / delete / delete [] ==70== at 0x483997B: free (in…
Robert Fraser
  • 10,649
  • 8
  • 69
  • 93
11
votes
1 answer

Making thread_local variables fully volatile

I'm working on a runtime library that uses user-level context switching (using Boost::Context), and am having trouble using thread_level variables. Consider the following (reduced) code: thread_local int* volatile tli; int main() { tli = new…
Eran
  • 21,632
  • 6
  • 56
  • 89
11
votes
4 answers

Is it OK to use ThreadLocal for storing the requested Locale?

I am working on internationalizing user entered data in a rather large Client/Server (HTTP (Hessian) is used for communication) application which is stored in a database. Users can choose the language they want to see and there is a default language…
yankee
  • 38,872
  • 15
  • 103
  • 162
10
votes
1 answer

How does pthread_key_t and the method pthread_key_create work?

I am having some trouble figuring out how pthread_key_t and pthread_key_create work. From my understand, each thread has TLS (thread local storage) and that a key is used to access the thread local storage. What I do not get is when a key is…
user972276
  • 2,973
  • 9
  • 33
  • 46
10
votes
0 answers

Why isn't a thread_local variable destroyed when the thread returns?

For a better understanding of this question, here is the code: // code 1 #include #include struct tls_test { tls_test() { std::cout << "tls_test ctor\n"; } ~tls_test() { std::cout << "tls_test dtor\n"; } …
iTruth
  • 123
  • 7
10
votes
8 answers

ThreadLocal to store ServletRequest and Response in servlet: what for?

Once I have came across a pattern, where ServletRequest and response objects are put to servlet's local ThreadLocal variables. The servlet class has also methods to get current request and response objects. So in order to get these objects you still…
glaz666
  • 8,707
  • 19
  • 56
  • 75
10
votes
3 answers

C++ - Where are thread_local variables stored?

I am trying to understand how exactly thread_local qualifier works and where the actual variable gets stored? This is on C++. Say I have a class with multiple member variables. An objet of the class is instantiated on heap and the object is shared…
Ben Connor
  • 103
  • 1
  • 6
10
votes
2 answers

Do you know of some performances test of the different ways to get thread local storage in C++?

I'm doing a library that makes extensive use of a thread local variable. Can you point to some benchmarks that test the performances of the different ways to get thread local variables in C++: C++0x thread_local variables compiler extension (Gcc…
Vicente Botet Escriba
  • 4,305
  • 1
  • 25
  • 39
10
votes
1 answer

How to initialize thread local variable in c++?

Possible Duplicate: C++11 thread_local in gcc - alternatives Is there any way to fully emulate thread_local using GCC's __thread? I wanted to use the c++11 thread_local to create and use thread_local variable but as it is not yet supported by…
polapts
  • 5,493
  • 10
  • 37
  • 49
9
votes
3 answers

Is ThreadLocal safe to use with Tomcat NIO Connector

This just came to mind when testing the Tomcat NIO connector during my load tests. I make use of ThreadLocal's additionally I use Spring, which I know in several places it also makes use of it. Since the NIO connector does not have a thread per…
David
  • 699
  • 7
  • 18
9
votes
3 answers

Java Cached thread pool and thread local

I have a question about java and concurrency. Let say I have a ThreadLocal variable called 'a'. And I use a CachedThreadPool to obtain new threads. When a thread is reused, what happens to the ThreadLocal variable 'a'? It maintains the same value…
Mateu
  • 2,636
  • 6
  • 36
  • 54
9
votes
1 answer

Do operations on ThreadLocal have to be synchronized?

Here is the code I've stumbled across: class TransactionContextHolder { private static final ThreadLocal currentTransactionContext = new NamedInheritableThreadLocal( "Test Transaction Context"); static…
9
votes
2 answers

How to get support for thread_local on Mac OSX clang?

As shown in this answer, clang from Xcode on Mac OSX does not support thread_local storage even with C++11 flags set. Even on the latest version, Apple LLVM version 7.0.0 (clang-700.1.76), Target: x86_64-apple-darwin15.0.0, Thread model: posix,…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295