当前位置:网站首页>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 ?
- 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 .
- 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;
}
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 helloNdx 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 4therefore 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 .
边栏推荐
- Vscade personalization: let a cute girl knock the code with you
- Binary String
- Prevent others from using the browser to debug
- JS to read the picture of the clipboard
- Extend your kubernetes API using the aggregation API
- JS prototype and prototype chain Paramecium can understand
- Explain the startup process of opengauss multithreading architecture in detail
- Debian10 create users, user groups, switch users
- [hdu] p7058 ink on paper finding the maximum edge of the minimum spanning tree
- Yyds dry goods counting tail recursion is better than recursion
猜你喜欢

Overview of visual object detection technology based on deep learning

Autumn move script B

Local deployment and problem solving of IIS in ArcGIS JS 4.23
Yyds dry goods counting tail recursion is better than recursion

SQL programming task04 job - set operation

層次選擇器

Development status of full color LED display

Day367: valid complete square

Zabbix5 series - use temperature and humidity sensor to monitor the temperature and humidity of the machine room (XX)

On AI and its future trend | community essay solicitation
随机推荐
3D printing microstructure
Pat class A - 1014 waiting in line (bank queuing problem | queue+ simulation)
Yyds dry goods counting tail recursion is better than recursion
Node fetch download file
Charles garbled code problem solving
使用aggregation API扩展你的kubernetes API
What aspects of language and database knowledge are needed to build a web Kanban!
Analysis of current mainstream video coding technology | community essay solicitation
You can also do NLP (classification)
魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
Day260: the number III that appears only once
JS - single sign on
[Title Fine brushing] 2023 Hesai FPGA
Char[], char *, conversion between strings
Install MySQL (5.7+8.0) through docker and configure master-slave replication (gtid+ enhanced semi synchronization)
Vector 2 (friend and copy construction)
SAP ui5 application development tutorial 102 - detailed trial version of print function implementation of SAP ui5 application
On AI and its future trend | community essay solicitation
Autumn move script C
SFOD:无源域适配升级优化,让检测模型更容易适应新数据