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 ...
随机推荐
- 模型构建<3>:交叉验证
交叉验证是模型比较选择的一种常用方法,本文对此进行总结梳理. 1.交叉验证的基本思想 交叉验证(cross validation)的基本思想就是重复地利用同一份数据. 2.交叉验证的作用 1)通过划分 ...
- Cipolla算法学习小记
转自:http://blog.csdn.net/doyouseeman/article/details/52033204 简介 Cipolla算法是解决二次剩余强有力的工具,一个脑洞大开的算法. 认真 ...
- 【POJ】2796:Feel Good【单调栈】
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 18449 Accepted: 5125 Case T ...
- Codeforces Round #234 (Div. 2) A. Inna and Choose Options 模拟题
A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...
- spring---aop(4)---Spring AOP的CGLIB动态代理
写在前面 前面介绍了Spring AOP的JDK动态代理的过程,这一篇文章就要介绍下Spring AOP的Cglib代理过程. CGLib全称为Code Generation Library,是一个强 ...
- 【Eclipse提高开发速度-插件篇】安装多语言Propertys编辑工具
安装的的时候能够通过 Help>Eclipse Marketplace的 搜索方式安装 或者 Help>Install New Software Name:Propertys URL:ht ...
- Hadoop 组成
这里介绍一下hadoop的组成, hadoop主要由两部分组成,,一个是hdfs,还有一个是mapreduce 这两个部分在hadoop 2.2.0中分别用start-dfs.sh和start-yar ...
- .NET:CLR via C# Thread Basics
A thread is a Windows concept whose job is to virtualize the CPU. Thread Overhead Thread kernel obje ...
- mysql访问权限GRANT ALL PRIVILEGES ON,访问权限表
开启远程连接:2, 修改 Mysql-Server 用户配置mysql> USE mysql; -- 切换到 mysql DBDatabase changedmysql> SELECT U ...
- python异常信息获取
1.python调试的时候获取异常信息 import traceback print '######################################################## ...