Thread-Local Storage for C99
线程本地存储(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;
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;
}
*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
pself = -1218581696,i = -1218581696
pself = -1226974400,i = 1
pself = -1226974400,i = -1226974400
Thread-Local Storage for C99的更多相关文章
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
原文链接地址:http://www.cppblog.com/Tim/archive/2012/07/04/181018.html 本文为线程本地存储TLS系列之分类和原理. 一.TLS简述和分类 我们 ...
- 线程本地存储(Thread Local Storage, TLS)简单分析与使用
在多线程编程中, 同一个变量, 如果要让多个线程共享访问, 那么这个变量可以使用关键字volatile进行声明; 那么如果一个变量不想使多个线程共享访问, 那么该怎么办呢? 呵呵, 这个办法就是TLS ...
- 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理
本文为线程本地存储TLS系列之分类和原理. 一.TLS简述和分类 我们知道在一个进程中,所有线程是共享同一个地址空间的.所以,如果一个变量是全局的或者是静态的,那么所有线程访问的是同一份,如果某一个线 ...
- 线程局部存储TLS(thread local storage)
同一全局变量或者静态变量每个线程访问的是同一变量,多个线程同时访存同一全局变量或者静态变量时会导致冲突,尤其是多个线程同时需要修改这一变量时,通过TLS机制,为每一个使用该全局变量的线程都提供一个变量 ...
- TLS Thread Local Storage
https://blog.csdn.net/yusiguyuan/article/details/22938671 https://blog.csdn.net/simsunny22/article/d ...
- TLS 与 python thread local
TLS 先说TLS( Thread Local Storage),wiki上是这么解释的: Thread-local storage (TLS) is a computer programming m ...
- 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 ...
- Ionic2学习笔记(8):Local Storage& SQLite
作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5557947.html Ionic2可以有两种方式来存储数据,Local S ...
- Web持久化存储Web SQL、Local Storage、Cookies(常用)
在浏览器客户端记录一些信息,有三种常用的Web数据持久化存储的方式,分别是Web SQL.Local Storage.Cookies. Web SQL 作为html5本地数据库,可通过一套API来操纵 ...
- cookie ,session Storage, local storage
先来定义: cookie:是网站为了标识用户身份存储在本地终端的数据,其数据始终在APP请求中存在,会在服务器和浏览器中来回传递 数据大小不超过4k, 可以设置有效期,过了有效期自动删除 sessio ...
随机推荐
- 【转】说下lua使用场景
[今日话题]说下lua使用场景 – flea 1. 我们有用,一些逻辑相对简单,没有复杂的数据交互,访问频次超高的接口实现,可以用lua,省得用phpfpm,太重,浪费资源. – 付坤 2. 也可 ...
- hdu 4857 逆向建图+拓扑排序 ***
题意:糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行.现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会是不平等的,这些人有的穷有 ...
- bzoj 4034
我写的是 DFS序+线段树 DFS序(出去的位置要单独建点)上,进入的位置是权值,出去的位置是权值的相反数,可以证明节点i到根节点的路径上的点的权值和是DFS序上1-in[i]的和. 只要搞出每个区间 ...
- js的继承实现方式
1. 使用call或者apply来实现js对象继承 function Animal(age){ this.age = age; this.say = function(){ console.log(' ...
- CentOS6.X关闭防火墙
一.关闭防火墙 1.重启后永久性生效: 开启:chkconfig iptables on 关闭:chkconfig iptables off 2.即时生效,重启后失效: 开启:service ipta ...
- Swift 内存管理
1.Object-C 经历两个阶段: 1.手动引用计数内存管理(Manual Reference Counting,MRC) 2.自动引用计数内存管理(Automatic Refernce Count ...
- 怎么改变html中placeholderr的文字颜色
<input type="text" placeholder="这里是提示文本" /> 设置的css代码: ::-webkit-input-plac ...
- Swift3.0字符串相关操作
以下有关字符串的常用操作都可直接复制到Xcode中进行验证,如发现错误,请在评论区留言指正! 1.字符串的定义 var str1="hello, swift." //字符串变量 相 ...
- DataTable添加行的方法
方法一: DataTable tblDatas = new DataTable("Datas");DataColumn dc = null;dc = tblDatas.Colum ...
- SOLARIS 11G 安装 ORACLE 11G
https://docs.oracle.com/cd/E11882_01/install.112/e48357/pre_install.htm#SSDBI1209 http://blog.chinau ...