我正在像这样从我的代码中的线程局部变量中读取数据,
// tid_local is declared as __thread int tid_local;
long tid = tid_local
查看反汇编代码,我看到了这样的东西,我怀疑这是通过读取tid_local 来分配tid 的指令。
movslq %fs:0xfffffffffffffffc,%rbx
现在我的问题是,这是否真的是执行此操作的指令,即从本地线程变量读取,以及 gcc 是否始终使用 fs 段来存储线程本地变量。这应该如何工作?
最佳答案
是的,这很可能是正确的指示。来自 gcc
manual :
-mtls-direct-seg-refs
-mno-tls-direct-seg-refs
Controls whether TLS variables may be accessed with offsets from the TLS segment register (%gs for 32-bit, %fs for 64-bit), or whether the thread base pointer must be added. Whether or not this is legal depends on the operating system, and whether it maps the segment to cover the entire TLS area.
编辑 这是@janneb 在评论中建议的一个很好的链接:http://www.akkadia.org/drepper/tls.pdf
关于c - 线程局部变量和 fs 段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8747070/