Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60487
  • 博文数量: 17
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 195
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-01 14:14
文章分类

全部博文(17)

文章存档

2011年(1)

2009年(16)

我的朋友

分类: LINUX

2009-04-20 10:43:15

Thread-Local Storage

Thread-local storage (TLS) is a mechanism by which variables are allocated such that there is one instance of the variable per extant thread. The run-time model GCC uses to implement this originates in the IA-64 processor-specific ABI, but has since been migrated to other processors as well. It requires significant support from the linker (ld), dynamic linker (ld.so), and system libraries (libc.so and libpthread.so), so it is not available everywhere.

At the user level, the extension is visible with a new storage class keyword: __thread. For example:

     __thread int i;
extern __thread struct state s;
static __thread char *p;

The __thread specifier may be used alone, with the extern or staticextern or static, __thread must appear immediately after the other storage class specifier. specifiers, but with no other storage class specifier. When used with

The __thread specifier may be applied to any global, file-scoped static, function-scoped static, or static data member of a class. It may not be applied to block-scoped automatic or non-static data member.

When the address-of operator is applied to a thread-local variable, it is evaluated at run-time and returns the address of the current thread's instance of that variable. An address so obtained may be used by any thread. When a thread terminates, any pointers to thread-local variables in that thread become invalid.

No static initialization may refer to the address of a thread-local variable.

In C++, if an initializer is present for a thread-local variable, it must be a constant-expression, as defined in 5.19.2 of the ANSI/ISO C++ standard.

See for a detailed explanation of the four thread-local storage addressing models, and how the run-time is expected to function.

阅读(803) | 评论(0) | 转发(0) |
0

上一篇:gdb调试多线程

下一篇:linux pthread

给主人留下些什么吧!~~