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

关键字

   __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. check_mk检测插件 - raid监控

    mk_raidstatus python版本 #!/usr/bin/env python # -*- encoding: utf-8; py-indent-offset: 4 -*- import s ...

  2. 2017.11.16 JavaWeb-------第八章 EL、JSTL、Ajax技术

    第八章 EL.JSTL.Ajax技术 ~~ EL (expression language) 是表达式语言 ~~ JSTL(JSP Standard Tag Library) 是开源的JSP标准标签库 ...

  3. Vuex基础-Action

    在文章开始之前,再次强调一句:Vuex会把getter mutations action不管是在模块定义的还是在根级别定义的 都会注册在全局 官网API地址:https://vuex.vuejs.or ...

  4. CDH4.5.0下安装lzo

    参考 http://www.cloudera.com/content/cloudera-content/cloudera-docs/Impala/1.0.1/Installing-and-Using- ...

  5. CentOS中配置php环境

    1.安装apache: yum install httpd httpd-devel   2.安装mysql: yum install mysql mysql-server   3.安装php: yum ...

  6. 第43章 RTC—实时时钟—零死角玩转STM32-F429系列

    第43章     RTC—实时时钟 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fireg ...

  7. input的placeholder的颜色、字号、边距设置

    #scbar_txt::-webkit-input-placeholder { /* WebKit browsers */    text-indent: 10px; font-size:16px; ...

  8. C# 委托知识总结【转】

    1.什么是委托,为什么要使用委托 我正在埋头苦写程序,突然想喝水,但是又不想自己去掉杯水而打断自己的思路,于是我就想让女朋友去给我倒水.她去给我倒水,首先我得让她知道我想让她干什么,通知她之后我可以继 ...

  9. mpvue项目中安装weui

    观察一下发现,mpvue项目打包css的规律是:根组件App.vue里的style样式全部打包到 dist  /  static / css / app.wxss ..   参照微信小程序的原生引入使 ...

  10. js动画之无缝滚动

    效果图如下: HTML代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...