当前位置:网站首页>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 .
边栏推荐
- Population standard deviation and sample standard deviation
- 使用aggregation API扩展你的kubernetes API
- Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data
- JS to paste pictures into web pages
- B tree and b+ tree
- Day500: keyboard line
- Phantomjs Usage Summary
- Yyds dry goods counting tail recursion is better than recursion
- Ansible learning summary (7) -- ansible state management related knowledge summary
- 層次選擇器
猜你喜欢

Ros2 summer school 2022 transfer-

Template specialization template <>

Explain the startup process of opengauss multithreading architecture in detail
![[hdu] P6964 I love counting](/img/ff/f8e79d28758c9bd3019816c8f46723.png)
[hdu] P6964 I love counting

Use elk to save syslog, NetFlow logs and audit network interface traffic

3D打印微组织

JS - single sign on

Autumn move script C

MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)
![[initial launch] there are too many requests at once, and the database is in danger](/img/c1/807575e1340b8f8fe54197720ef575.png)
[initial launch] there are too many requests at once, and the database is in danger
随机推荐
SFOD:无源域适配升级优化,让检测模型更容易适应新数据
3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World
JS - single sign on
SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
The road of architects starts from "storage selection"
Learn the specific technical learning experience of designing reusable software from the three levels of class, API and framework
Requête linq
Get the start and end dates of the current week
[Luogu] P2887 Sunscreen G
JS prototype and prototype chain Paramecium can understand
关于打算做一个web的问题看板,需要使用哪些方面语言及数据库知识!
A blog allows you to understand the use of material design
Up the Strip
leetcode 91. Decode ways (medium)
Thead safety experience
3D打印微组织
MySQL-Seconds_ behind_ Master accuracy error
[ZOJ] P3228 Searching the String
C. Number of Pairs-Codeforces Round #725 (Div. 3)
Big guys, 2.2.1 the CDC monitoring SQLSEVER can only get the full amount of data. Why can't we get the incremental data in the later period