加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

UNIX情形高级编程:线程私稀有据

发布时间:2016-11-15 23:52:24 所属栏目: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.

(编辑:源码网)

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

热点阅读