当前位置:网站首页>Thread local storage understanding

Thread local storage understanding

2022-06-23 01:38:00 summer_ sunrise

Thread Local Storage understand

Learn with problems

1. What is? thread local storage?

A method of computer programming , Use thread local static or global memory .

2. What's the main function ?

  1. Avoid resource competition ; When multiple threads access the same resource , There will be competition . When a resource is declared as thread local storage when , There will be no competition .
  2. Use the reentrancy of global object methods ; For example, a function uses global variables to set an error code ( such as c In the library errno), If errno It's a global variable , A system method will override the value that has just been modified by another thread , However, the code of another thread is right again errno For verification ( At this point, the results do not meet expectations ); Use thread local storage Can solve : send errno It looks like a global variable , But each thread has one .

3. What segment is stored after compilation ?

After compilation, it is stored in TLS section, If there is no initial value , It's in .tbss; If there is an initial value , It's in .tdata;

#include <stdio.h>
#include <thread>

thread_local int hello = 3;
int main(int argc, char** argv){
    printf("hello:%d\n",hello);
    return 0;
}
  1. Use readelf -s see hello The position of the symbol ( First compile the above code into main Binary system )

    readelf -s main | grep -E ‘hello|Num’

    Output :

       Num:    Value          Size Type    Bind   Vis      Ndx Name
       Num:    Value          Size Type    Bind   Vis      Ndx Name
        58: 0000000000000000     4 TLS     GLOBAL DEFAULT   21 hello
    
  2. Ndx Indicates the segment to which the symbol belongs , Use readelf -S see 21 What paragraph is it

    readelf -S main | grep -A 1 -E ‘[21|Name’

    Output

      [Nr] Name              Type             Address           Offset
           Size              EntSize          Flags  Link  Info  Align
    --
      [21] .tdata            PROGBITS         0000000000003db4  00002db4
           0000000000000004  0000000000000000 WAT       0     0     4
    

    therefore thread_local Variable hello The symbol type of is TLS, Global scope , be located .tdata In the paragraph .

ps: When thread_local When a variable is not initialized , be located .tbss paragraph ( Similar to global variables , The initialized global variables are located in .data paragraph , Uninitialized global variables are located in .bss paragraph ), Those who are interested can make thread_local int hello = 3; Change it to thread_local int hello; Then follow the above steps to view .

What segment is stored after compilation ?

answer : The initialization of the thread_local After the variable is compiled, it is located in .tdata paragraph , Uninitialized at .tbss paragraph .

4. How the runtime initializes , What area of memory is it stored in ?

When a thread uses a variable , Copy data from the corresponding data segment , Then store it in thread local storage Area .

原网站

版权声明
本文为[summer_ sunrise]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220515021942.html