线程本地存储(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. luoguP4492 [HAOI2018]苹果树 组合计数 + dp

    首先,每个二叉树对应着唯一的中序遍历,并且每个二叉树的概率是相同的 这十分的有用 考虑\(dp\)求解 令\(f_i\)表示\(i\)个节点的子树,根的深度为\(1\)时,所有点的期望深度之和(乘\( ...

  2. 4144: [AMPPZ2014]Petrol (多源最短路+最小生成树+启发式合并)

    4144: [AMPPZ2014]Petrol Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 752  Solved: 298[Submit][Sta ...

  3. 洛谷.3803.[模板]多项式乘法(NTT)

    题目链接:洛谷.LOJ. 为什么和那些差那么多啊.. 在这里记一下原根 Definition 阶 若\(a,p\)互质,且\(p>1\),我们称使\(a^n\equiv 1\ (mod\ p)\ ...

  4. python开发_csv(Comma Separated Values)_逗号分隔值_常用导入导出格式_完整版_博主推荐

    ## 最近出了一趟差,是从20号去的,今天回来...# 就把最近学习的python内容给大家分享一下...#''' 在python中,CSV(Comma Separated Values),从字面上面 ...

  5. 怎么改变html中placeholderr的文字颜色

    <input type="text" placeholder="这里是提示文本" /> 设置的css代码: ::-webkit-input-plac ...

  6. JS实现背景透明度可变,文字不透明的效果

    最近项目里需要实现这么个功能,类似网游中的聊天框,背景都是透明的,但是文字是不透明.所以如果简单的使用opacity(非IE)和alpha滤镜(IE)是无法实现这个效果的,会造成全部透明. 解决办法如 ...

  7. 使用Django来处理对于静态文件的请求

    引言 本方法适用于linux+python2.7+django1.2,使用django自带的web服务. 同样也适用于sina app engine. 1.准备工作 准备css文件,准备js文件,准备 ...

  8. Tasker to detect and vibrate once the ougoing call is being answered

    I happen to find that for GSM standard phone, call duration would be created into sql database only ...

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

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

  10. GDB 自动化操作的技术-PYTHON

    https://github.com/spacewander/debugger-utils http://python.jobbole.com/85415/ https://segmentfault. ...