线程本地存储(TLS)是一种机制,通过这样的机制进行变量分配。在每一个现存线程都有一个实例变量。这样的执行模型GCC用来实现这个,起源于IA-64处理器,可是已经被迁移到其它的处理器。它须要大量的支持连接器(ld)、动态连接器(ld.so)和系统库(libc.so和libpthread.so),所以不是到处都可用的。

在用户层,一个新的存储类扩展keyword:__thread.比如:

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

这个keyword__thread能够单独使用。也能够和extern或者static配合使用,不能与其它的存储类说明符使用。当使用extern或者static。__thread必须在这些存储keyword后面使用。

看以下的样例:

/*File : thread.c
*Auth : sjin
*Date : 20141023
*Mail : 413977243@qq.com
*/ #include <stdio.h>
#include <pthread.h> #define MAX_THREADS 2 static __thread int i = 1; void *thr_func(void *arg)
{
printf("pself = %d,i = %d\n",pthread_self(),i);
i = ( int)pthread_self();
printf("pself = %d,i = %d\n",pthread_self(),i);
} int main()
{
int j = 0;
pthread_t thread_id[MAX_THREADS]; for(j = 0; j < MAX_THREADS;j++){
pthread_create(&thread_id[j],0,thr_func,NULL);
} for(j = 0; j < MAX_THREADS;j++){
pthread_join(thread_id[j],NULL);
} return 0;
}

执行结果:

pself = -1218581696,i = 1
pself = -1218581696,i = -1218581696
pself = -1226974400,i = 1
pself = -1226974400,i = -1226974400

參考资料:
 1、https://bugs.gentoo.org/show_bug.cgi?id=18316
2、https://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html

Thread-Local Storage for C99的更多相关文章

  1. 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理

    原文链接地址:http://www.cppblog.com/Tim/archive/2012/07/04/181018.html 本文为线程本地存储TLS系列之分类和原理. 一.TLS简述和分类 我们 ...

  2. 线程本地存储(Thread Local Storage, TLS)简单分析与使用

    在多线程编程中, 同一个变量, 如果要让多个线程共享访问, 那么这个变量可以使用关键字volatile进行声明; 那么如果一个变量不想使多个线程共享访问, 那么该怎么办呢? 呵呵, 这个办法就是TLS ...

  3. 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理

    本文为线程本地存储TLS系列之分类和原理. 一.TLS简述和分类 我们知道在一个进程中,所有线程是共享同一个地址空间的.所以,如果一个变量是全局的或者是静态的,那么所有线程访问的是同一份,如果某一个线 ...

  4. 线程局部存储TLS(thread local storage)

    同一全局变量或者静态变量每个线程访问的是同一变量,多个线程同时访存同一全局变量或者静态变量时会导致冲突,尤其是多个线程同时需要修改这一变量时,通过TLS机制,为每一个使用该全局变量的线程都提供一个变量 ...

  5. TLS Thread Local Storage

    https://blog.csdn.net/yusiguyuan/article/details/22938671 https://blog.csdn.net/simsunny22/article/d ...

  6. TLS 与 python thread local

    TLS 先说TLS( Thread Local Storage),wiki上是这么解释的: Thread-local storage (TLS) is a computer programming m ...

  7. Java Thread Local – How to use and code sample(转)

    转载自:https://veerasundar.com/blog/2010/11/java-thread-local-how-to-use-and-code-sample/ Thread Local ...

  8. Ionic2学习笔记(8):Local Storage& SQLite

    作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5557947.html              Ionic2可以有两种方式来存储数据,Local S ...

  9. Web持久化存储Web SQL、Local Storage、Cookies(常用)

    在浏览器客户端记录一些信息,有三种常用的Web数据持久化存储的方式,分别是Web SQL.Local Storage.Cookies. Web SQL 作为html5本地数据库,可通过一套API来操纵 ...

  10. cookie ,session Storage, local storage

    先来定义: cookie:是网站为了标识用户身份存储在本地终端的数据,其数据始终在APP请求中存在,会在服务器和浏览器中来回传递 数据大小不超过4k, 可以设置有效期,过了有效期自动删除 sessio ...

随机推荐

  1. luoguP2231 [HNOI2002]跳蚤

    题目链接 bzoj1220: [HNOI2002]跳蚤 题解 根据裴蜀定理,不定方程的解为未知数的gcd,所以选取的n个数的gcd为1 那么n - 1个数保证没有公约数为m的约数,枚举质因数容斥 质因 ...

  2. 【10.17校内测试】【二进制数位DP】【博弈论/预处理】【玄学(?)DP】

    Solution 几乎是秒想到的水题叻! 异或很容易想到每一位单独做贡献,所以我们需要统计的是区间内每一位上做的贡献,就是统计区间内每一位是1的数的数量. 所以就写数位dp辣!(昨天才做了数字统计不要 ...

  3. 【洛谷】4304:[TJOI2013]攻击装置【最大点独立集】【二分图】2172: [国家集训队]部落战争【二分图/网络流】【最小路径覆盖】

    P4304 [TJOI2013]攻击装置 题目描述 给定一个01矩阵,其中你可以在0的位置放置攻击装置. 每一个攻击装置(x,y)都可以按照“日”字攻击其周围的8个位置(x-1,y-2),(x-2,y ...

  4. 安卓中WebKit的使用

    1.在安卓开发中,使用webkit显示网页 步骤: ①初始化一个webkit控件: ②获取webkit的WebSettings对象: ③设置javascript为enable ④为webkit设置&q ...

  5. ThinkPHP实现登录限制时__construct和_initialize的区别

    ThinkPHP支持两种构造方法:  __construct和_initialize(ThinkPHP内置的构造方法). 测试URL为:  http://oa.com/index.php/Admin/ ...

  6. Apache参数的优化(转)

    按照前面提到的版本问题,Apache可以直接使用2.0版本产品线.针对Apache的优化主要是针对httpd.conf的优化,当然还有其他地方,如果特别留意的话,网上常有专家惊呼“居然这么多人忽略xx ...

  7. Redhat Enterprise Linux 7.4/CentOS 7.4 安装后初始化配置

    由于我是最小化安装,需要在安装后进行一些配置 1. 设定启动级别 [root@home ~]# systemctl set-default multi-user.target 2. 设定网络 [roo ...

  8. 预装Windows 8系统机型如何进行一键恢复

    http://support1.lenovo.com.cn/lenovo/wsi/htmls/detail_20131119141246845.html

  9. How to Make Portable Class Libraries Work for You

    A Portable Class Library is a .NET library that can be used (in binary form, without recompiling) on ...

  10. ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th

    MSDN===http://msdn.microsoft.com/en-us/library/aa479332.aspx PROJECT==https://code.google.com/p/elma ...