Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 between 2 threads. Appropriate locking mechanism is used to ensure two threads are not stomping on a member variable at same time.

There is a need for threads to keep track of few thread specific items. So I am thinking to create a thread_local variable in same header file as the Class declaration. As I understand, both threads will get their own copy of this variable, correct? Where exactly is the thread local variable stored in memory? If data segment, how exactly does the right variable gets picked up during execution?

like image 749
Ben Connor Avatar asked Aug 23 '17 19:08

Ben Connor


People also ask

Where do variables get stored in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

Where are dynamic variables stored in C?

The heap. The remainder of the dynamic storage area is commonly allocated to the heap, from which application programs may dynamically allocate memory, as required. In C, dynamic memory is allocated from the heap using some standard library functions.

Where do variables get stored?

Most variables stored in the array (i.e., in main memory) are larger than one byte, so the address of each variable is the index of the first byte of that variable. Viewing main memory as an array of bytes. Main memory, often called RAM, can be visualized as a contiguous array of bytes.

What is thread local variables and where they are stored?

Thread local can be considered as a scope of access like session scope or request scope. In thread local, you can set any object and this object will be local and global to the specific thread which is accessing this object. Java ThreadLocal class provides thread-local variables.


2 Answers

In the case of 64 bit Windows, the TLS can be accessed via the GS selector register, using a separate physical address space per thread (allocated during CreateThread()), although Visual Studio may map the TLS into a process / thread virtual address space, with each thread getting a different virtual address, since it's a different physical address for each thread. You can look at the disassembled code by stepping into rand() with a debugger to see how it accesses the seed, which is a TLS variable.

like image 186
rcgldr Avatar answered Oct 07 '22 13:10

rcgldr


1. As I understand, both threads will get their own copy of this variable, correct?
Yes. Each thread gets its own copy of the thread_local variable.
2. Where exactly is the thread local variable stored in memory? If data segment, how exactly does the right variable gets picked up during execution?
thread_local is the implementation of the Thread Local Storage concept. TLS is implemented as a table of slots withing each thread object. Each thread having its own copy of the table. For e.g in Windows implementation of TLS this table is within the Thread Information Block of a thread. When a global/static variable is declared as thread_local, it would get associated with a table slot of each thread at the same offset. When the thread_local variable is accessed by a thread then using the current thread context, the thread's own copy of the variable associated with the table slot within this thread object is accessed. Please check this link for more detail on TLS implementation. https://en.wikipedia.org/wiki/Thread-local_storage

like image 7
Amit Rastogi Avatar answered Oct 07 '22 11:10

Amit Rastogi