Questions tagged [thread-local-storage]

Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread

Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread.

This is sometimes needed because normally all threads in a process share the same address space, which is sometimes undesirable. In other words, data in a static or global variable is normally always located at the same memory location, when referred to by threads from the same process. Variables on the call stack however are local to threads, because each thread has its own stack, residing in a different memory location.

Sometimes it is desirable that two threads referring to the same static or global variable are actually referring to different memory locations, thereby making the variable thread-local, a canonical example being the C error code variable error number.

If it is possible to make at least a memory address sized variable thread-local, it is in principle possible to make arbitrarily sized memory blocks thread-local, by allocating such a memory block and storing the memory address of that block in a thread-local variable.

Reference

315 questions
183
votes
3 answers

What does the thread_local mean in C++11?

I am confused with the description of thread_local in C++11. My understanding is, each thread has unique copy of local variables in a function. The global/static variables can be accessed by all the threads (possibly synchronized access using…
polapts
  • 5,493
  • 10
  • 37
  • 49
109
votes
4 answers

Are C++11 thread_local variables automatically static?

Is there a difference between these two code segments: void f() { thread_local vector V; V.clear(); ... // use V as a temporary variable } and void f() { static thread_local vector V; V.clear(); ... // use V as a…
Zuza
  • 2,136
  • 4
  • 20
  • 22
90
votes
5 answers

Thread local storage in Python

How do I use thread local storage in Python? Related What is “thread local storage” in Python, and why do I need it? - This thread appears to be focused more on when variables are shared. Efficient way to determine whether a particular function is…
Casebash
  • 114,675
  • 90
  • 247
  • 350
57
votes
9 answers

How to allocate thread local storage?

I have a variable in my function that is static, but I would like it to be static on a per thread basis. How can I allocate the memory for my C++ class such that each thread has its own copy of the class…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
40
votes
2 answers

Does Go have something like ThreadLocal from Java?

I use Go and Gin to setup my website and want to know the database access time. I use goroutine so if don't use something like thread-local, I must change almost every function to do it. Does Go have a good way to do it?
modkzs
  • 1,369
  • 4
  • 13
  • 17
39
votes
6 answers

Why is thread local storage so slow?

I'm working on a custom mark-release style memory allocator for the D programming language that works by allocating from thread-local regions. It seems that the thread local storage bottleneck is causing a huge (~50%) slowdown in allocating memory…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
39
votes
1 answer

Linux's thread local storage implementation

__thread Foo foo; How is foo actually resolved? Does the compiler silently replace every instance of foo with a function call? Is foo stored somewhere relative to the bottom of the stack, and the compiler stores this as "hey, for each thread, have…
anon
  • 41,035
  • 53
  • 197
  • 293
31
votes
2 answers

How is GCC's __thread implemented?

How is __thread in gcc implemented? Is it simply a wrapper over pthread_getspecific and pthread_setspecific? With my program that uses the posix API for TLS, I'm kind of disappointed now seeing that 30% of my program runtime is spent on…
user3810155
28
votes
1 answer

What is the difference between log4net.ThreadContext and log4net.LogicalThreadContext?

UPDATED on 11/18/2014 - While browsing the log4net source repository, I found that the implementation of LogicalThreadContext was modified in November 2011 to that it stores its properties using CallContext.LogicalSetData (and gets them using…
wageoghe
  • 27,390
  • 13
  • 88
  • 116
25
votes
2 answers

Why does `std::exit` not trigger destructors as expected?

#include #include #include #include using namespace std; using namespace std::literals; struct A { int n_ = 0; A(int n) : n_(n) { cout << "A:" << n_ << endl; } ~A() { cout << "~A:" << n_ << endl;…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
24
votes
4 answers

Access thread-local from another thread

How can I read/write a thread local variable from another thread? That is, in Thread A I would like to access the variable in Thread B's thread local storage area. I know the ID of the other thread. The variable is declared as __thread in GCC.…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
23
votes
3 answers

What is %gs in Assembly

void return_input (void) { char array[30]; gets (array); printf("%s\n", array); } After compiling it in gcc, this function is converted to the following Assembly code: push %ebp mov %esp,%ebp sub $0x28,%esp mov …
Alex F
  • 42,307
  • 41
  • 144
  • 212
23
votes
2 answers

The Cost of thread_local

Now that C++ is adding thread_local storage as a language feature, I'm wondering a few things: What is the cost of thead_local likely to be? In memory? For read and write operations? Associated with that: how do Operating Systems usually…
John
  • 2,326
  • 1
  • 19
  • 25
18
votes
3 answers

How does a C++ compiler implement thread local storage in C++0x?

How does c++ complier implement thread local storage in C++0x I have searched this in google. But I can't find anything about this. Does anyone have any material about this ??
Yuncy
  • 761
  • 1
  • 9
  • 20
18
votes
4 answers

c++ thread-local storage clang-503.0.40 (Mac OSX)

After I declared a variable in this way: #include namespace thread_space { thread_local int s; } //etc. i tried to compile my code using 'g++ -std=c++0x -pthread [sourcefile]'. I get the following error: example.C:6:8:…
pier94
  • 277
  • 1
  • 2
  • 8
1
2 3
20 21