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?
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).
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With