什么是线程本地存储?我们为什么需要它? [英] What is thread local storage? Why we need it?

查看:99
本文介绍了什么是线程本地存储?我们为什么需要它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读OS概念中的线程,并且遇到了线程本地存储(TLS)".我了解的是,TLS与静态或全局数据类似,但对于单个线程更独特.有点困惑这里有什么独特之处?

I am reading the Threads in OS concepts and i come across "thread local storage (TLS)". What I understood is that the TLS is similar to static or global data, but it is more unique to individual thread. Its bit confusing on what is unique here?

为什么我们不能通过运行程序(即线程的实际代码)函数将数据作为该函数的参数传递?

Why can't we pass the data through runner (i.e., thread's actual codes) functions as params to this function?

推荐答案

静态数据和全局数据在所有线程之间共享.如果修改了全局/静态变量,则所有线程均可见.如果您在TLS中创建变量,则与全局/共享变量不同,每个线程都有其自己的变量副本,即,对变量的更改是线程本地的.与通过 ds 段进行访问的全局变量不同,使用( gs/fs )段访问TLS变量.了解它的一个好方法是查看由编译器生成的反汇编.

Static and global data are shared across all the threads. If you modified a global/static variable it is visible to all the threads. Unlike global/shared variable if you create a variable in TLS, every thread has its own copy of the variable, i.e. changes to the variable is local to the thread. Unlike global variable where an access is made through ds segment, the TLS variable are accessed using (gs/fs) segment. A good way to learn about it is to look at the disassembly generated by the compiler.

这篇关于什么是线程本地存储?我们为什么需要它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆