线程存储原理:为变量在每一个现存的线程里分配一个实例,需要处理器支持,并不是所有都支持!支持全局的,静态的变量,但不支持局部变量。

关键字

   __thread   __thread int i;

    extern __thread struct state s;

   static __thread char *p;

测试  

   

#include<pthread.h>
#include<unistd.h>
#include<string.h>
#include <stdio.h> __thread int var=; typedef struct context_t
{
int p1;
char name[];
}context; __thread context ctx; void product(char *name)
{
static __thread int i = ;
printf("--->%s:%d\n",name, ++i);
} void* worker1(void*arg);
void* worker2(void*arg); int main()
{
pthread_t pid1,pid2;
memset(&ctx, , sizeof(context));
ctx.p1 = ;
strcpy(ctx.name, "null"); pthread_create(&pid1,NULL,worker1,NULL);
pthread_create(&pid2,NULL,worker2,NULL);
pthread_join(pid1,NULL);
pthread_join(pid2,NULL); printf("exit, var:%d, p1:%d, name=%s \n",var,ctx.p1, ctx.name); return ;
}
void* worker1(void* arg)
{
strcpy(ctx.name, "lilei");
ctx.p1=;
printf("work1:%d \n", ++var);
printf("work1:%d, name=%s\n", ctx.p1, ctx.name);
product("work1");
}
void* worker2(void* arg)
{
sleep();
printf("work2:%d \n", ++var);
printf("work2:%d, name=%s\n", ctx.p1, ctx.name);
product("work2");
}

输出结果:

[root@localhost test]# gcc -lpthread -o test test.c
[root@localhost test]# ./test
work1:
work1:, name=lilei
--->work1:
work2:
work2:, name=
--->work2:
exit, var:, p1:, name=null

可以看出多个线程调用,但是var,ctx, i变量值显示都是线程独立的。

参考 http://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Thread_002dLocal.html

另一种 TLS 通过pthread_key_create实现 http://www.cnblogs.com/gogly/articles/2416362.html

Thread-local storage (TLS)的更多相关文章

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

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

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

    原文链接地址:http://www.cppblog.com/Tim/archive/2012/07/04/181018.html 本文为线程本地存储TLS系列之分类和原理. 一.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. git 因线上分支名重复导致无法拉取代码

    有时 git pull 或 git fetch 时发现 git 报了个异常,说法像是无法将线上某个分支与本地分支合并,由于分支是...(很长的hash)但是分支却是...(很长的hash) 仔细查查后 ...

  2. java工程师要求

    高级JAVA研发工程师 8000-15000元/月 职位信息 职位描述 岗位要求:1.五年以上软件研发经验,两年以上软件架构设计经验:2.精通weblogic.jboss tomcat.websphe ...

  3. Apache Module mod_ssl

    http://httpd.apache.org/docs/current/mod/mod_ssl.html Description: Strong cryptography using the Sec ...

  4. 【转载】#336 - Declaring and Using a readonly Field

    You can make a field in a class read-only by using the readonly modifier when the field is declared. ...

  5. 【CCPC-Wannafly Winter Camp Day4 (Div1) C】最小边覆盖(简单题)

    点此看题面 大致题意: 给你一个边集的子集,问你这可不可能是这张图的最小边覆盖. 大致思路 考虑到,如果一条边连接的两个点度数都大于等于\(2\),则这条边完全可以删去. 因此,我们只要判断是否存在这 ...

  6. linux命令之awk命令

    awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是linu ...

  7. Family Gathering at Christmas(思维题)

    Family Gathering at Christmas 时间限制: 1 Sec  内存限制: 128 MB提交: 13  解决: 4[提交] [状态] [讨论版] [命题人:admin] 题目描述 ...

  8. bootstarp v3 学习简记

    1.快速设置浮动通过这两个class让页面元素左右浮动. !important被用来避免某些问题. <div class="pull-left">...</div ...

  9. 动态生成的DOM做点击事件无效

    有时候我们的标签都是从后台获取的数据,然后利用JS添加到页面上,当我们写生成的标签的点击事件(click)时没有效果. 例如: <section> 测试动态生成的DOM点击事件 <b ...

  10. React后台管理系统-品类的增加、修改和查看

    1.页面 2.品类列表展示 let listBody = this.state.list.map((category, index) => {             return (      ...