Thread-Specific Data(线程私有数据)
线程私有数据(也称线程特有数据)是存储和查询与某个线程相关的数据的一种机制。把这种数据称为线程私有数据或特定数据的原因是,希望每个线程可以独立地访问数据副本,而不需要担心与其他线程的同步访问问题。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>pthread_key_t key_handle;typedef struct tag_tsd_data
{int iData;//TODO
}STD_DATA;void destructor()
{printf("pthread_key destructor.\n");
}int tsd_key_init()
{int iRet = 0;iRet = pthread_key_create(&key_handle, destructor);if(iRet != 0){printf("pthread_key_create error.\n");}return iRet;
}STD_DATA* get_thread_std()
{STD_DATA* pStd = NULL;pStd = pthread_getspecific(key_handle);if(pStd == NULL){pStd = (STD_DATA*)malloc(sizeof(STD_DATA));if(pStd == NULL){printf("malloc error.\n");return NULL;}memset(pStd, 0, sizeof(STD_DATA));pthread_setspecific(key_handle, pStd);}return pStd;
}void *thread_fun2(void *arg)
{STD_DATA *pStd = NULL;printf("thread [%d] is running...\n", (int)pthread_self());pStd = get_thread_std();if(pStd == NULL){printf("fun1 get std error.\n");return NULL;}pStd->iData = (int)pthread_self();printf("fun 2 thread [%d], thread specific data [%d].\n", (int)pthread_self(), pStd->iData);return NULL;
}void* thread_fun1(void *arg)
{pthread_t tid;STD_DATA *pStd = NULL;printf("thread [%d] is running...\n", (int)pthread_self());pthread_create(&tid, NULL, thread_fun2, 0);pStd = get_thread_std();if(pStd == NULL){printf("fun1 get std error.\n");return NULL;}pStd->iData = (int)pthread_self();printf("fun 1 thread [%d], thread specific data [%d].\n", (int)pthread_self(), pStd->iData);sleep(3);return NULL;
}int main(int argc, char *argv[])
{pthread_t tid;int iRet = -1;iRet = tsd_key_init();if(iRet){printf("init tsd key error.\n");return 0;}STD_DATA* pStd = NULL;pStd = get_thread_std();if(pStd == NULL){printf("get specific std error.\n");return 0;}pStd->iData = (int)pthread_self();printf("main thread [%d], thread specific data [%d].\n", (int)pthread_self(), pStd->iData);iRet = pthread_create(&tid, NULL, thread_fun1, 0);if(iRet != 0){printf("pthread_create error.\n");return 0;}sleep(5);pthread_key_delete(key_handle);return 0;
}
Thread-Specific Data(线程私有数据)相关推荐
- TSD(Thread Specific Data)线程专有数据
(1)全局变量 (2)局部变量 (3)TSD(Thread-Specific Data 线程专有数据) 1.http://upczap.itpub.net/ 在单线程的程序里,有两种基本的数据:全局变 ...
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...
- 【Linux系统编程】线程私有数据
00. 目录 文章目录 00. 目录 01. 线程之间共享数据 02. 线程私有数据 2.1 创建线程私有数据 2.2 销毁线程私有数据 2.3 关联线程私有数据成员 2.4 读取线程私有数据所关联的 ...
- 【C/C++多线程编程之十】pthread线程私有数据
多线程编程之线程私有数据 Pthread是 POSIX threads 的简称,是POSIX的线程标准. 线程同步从互斥量[C/C++多线程编程之六]pthread互斥量,信号量[C/C++多线程编 ...
- 【Android 内存优化】Java 内存模型 ( Java 虚拟机内存模型 | 线程私有区 | 共享数据区 | 内存回收算法 | 引用计数 | 可达性分析 )
文章目录 一. Java 虚拟机内存模型 二. 程序计数器 ( 线程私有区 ) 三. 虚拟机栈 ( 线程私有区 ) 四. 本地方法栈 ( 线程私有区 ) 五. 方法区 ( 共享数据区 ) 1. 方法区 ...
- POSIX线程专有数据的空间释放问题,pthread_key_create
下面说一下线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了?大家都知道,在多线程程序中,所有线程共享程序中的变量.现在有一全局变量,所有线程都可以 ...
- 多线程私有数据pthread_key_create
在多线程的环境下,进程内的所有线程共享进程的数据空间.因此全局变量为所有线程共享.在程序设计中有时需要保存线程自己的全局变量,这种特殊的变量仅在线程内部有效. 如常见的errno,它返回标准的错误码. ...
- Linux多线程实践(4) --线程特定数据
线程特定数据 int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)); int pthread_key_ ...
- Qt使用 std::thread 线程插入数据到 QTableWidget
Qt使用 std::thread 线程插入数据到 QTableWidget中 一.实现效果 二.主要代码 1. ThreadTable.h 头文件 2. ThreadTable.cpp 源文件 3. ...
- android字符串获取数字索引,从字符串中提取特定数据(Extract specific data from a string)...
从字符串中提取特定数据(Extract specific data from a string) 我有一个带有描述的长字符串. 我想从字符串中提取一些信息. 但我无法弄明白该怎么做. 这是字符串: C ...
最新文章
- 面试官:听说你精通并发编程,来说说你对ThreadLocal的理解
- python好学吗mooc中文网-Python的N种玩法_中国大学MOOC(慕课)
- [当人工智能遇上安全] 1.人工智能真的安全吗?浙大团队外滩大会分享AI对抗样本技术
- 微信企业号三个连接模式
- python亿级mysql数据库导出_Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法...
- 新东方私有化背后的秘密
- pandas—pd.merge通过键来联接数据集
- 使用Palette来对图片进行颜色提取
- 【译】2018 年前端开发回顾
- 数字图像处理期末复习题
- 中文分词(上)——获取和Word2Vec模型构建
- 虚拟机出现entering emergency mode,使用xfs_rapair出现Device or resource busy解决
- 用grldr启动ISO
- 3DES加密,苹果、Java 、安卓 平台一致的加密工具
- IReport 图形化报表开发工具
- 微信小程序:图片404错误,更换默认图片
- 1-2 二十四点 (20 分)【Csp认证真题】
- DevOps工程师主要负责哪些事?需要具备哪些技能?
- 猜数字游戏(数字炸弹)
- 【视频播放器】potplayer调教教程
热门文章
- React Native 程序部署至 iOS 应用商店之前需要的配置和如何生成 release 版本的 APK 包
- c语言竖线什么意思,竖线符号意思
- 自然语言处理 cs224n 2019 Lecture 11: ConvNets for NLP
- java实现模拟时钟表盘
- 微信视频号下载视频工具3.0,实测有效免费保存!
- UVA - 10410 -通过dfs序列和bfs序列还原树
- obj文件(1):obj文件用txt打开并且了解v,f,vn,vt的含义
- C语言wifi程序代码,STM32F103 WIFI程序 C语言.docx
- 基于安卓android studio的日记APP 笔记APP或者备忘录APP 设计开发
- 用户验收测试要求目标