UNIX情形高级编程:线程私稀有据
运行结果: [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. (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |