加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

UNIX环境高级编程:线程私有数据

发布时间:2016-09-27 17:04:36 所属栏目:Unix 来源:站长网
导读:副标题#e# 线程私有数据(Thread-specific data,TSD):存储和查询与某个线程相关数据的一种机制。 在进程内的所有线程都共享相同的地址空间,即意味着任何声明为静态或外部变量,或在进程堆声明的变量,都可以被进程内所有的线程读写。 一个线程真正拥有的

运行结果:

[cpp] view plaincopyprint

01.huangcheng@ubuntu:~$ ./a.out

02.Initializing key

03.thread 2 set tsd value at 0x8fb7520

04.thread 2 starting......

05.thread 1 set tsd value at 0x8fb7530

06.thread 1 starting......

07.thread 2 done......

08.thread 1 done......

huangcheng@ubuntu:~$ ./a.out

Initializing key

thread 2 set tsd value at 0x8fb7520

thread 2 starting......

thread 1 set tsd value at 0x8fb7530

thread 1 starting......

thread 2 done......

thread 1 done......

示例代码3:

[cpp] view plaincopyprint

01.#include <stdio.h>

02.#include <stdlib.h>

03.#include <pthread.h>

04.

05.pthread_key_t key;

06.

07.struct test_struct {

08. int i;

09. float k;

10.};

11.

12.

13.void *child1 (void *arg)

14.{

15. struct test_struct struct_data;

16.

17. struct_data.i = 10;

18. struct_data.k = 3.1415;

19.

20. pthread_setspecific (key, &struct_data);

21. printf ("结构体struct_data的地址为 0x%pn", &(struct_data));

22. printf ("child1 中 pthread_getspecific(key)返回的指针为:0x%pn", (struct test_struct *)pthread_getspecific(key));

23.

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读