UNIX情形高级编程:线程私稀有据
printf ("利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值:nstruct_data.i:%dnstruct_data.k: %fn", ((struct test_struct *)pthread_getspecific (key))->i, ((struct test_struct *)pthread_getspecific(key))->k); printf ("------------------------------------------------------n"); } void *child2 (void *arg) { int temp = 20; sleep (2); printf ("child2 中变量 temp 的地址为 0x%pn", &temp); pthread_setspecific (key, &temp); printf ("child2 中 pthread_getspecific(key)返回的指针为:0x%pn", (int *)pthread_getspecific(key)); printf ("利用 pthread_getspecific(key)打印 child2 线程中与key关联的整型变量temp 值:%dn", *((int *)pthread_getspecific(key))); } int main (void) { pthread_t tid1, tid2; pthread_key_create (&key, NULL); pthread_create (&tid1, NULL, (void *)child1, NULL); pthread_create (&tid2, NULL, (void *)child2, NULL); pthread_join (tid1, NULL); pthread_join (tid2, NULL); pthread_key_delete (key); return (0); } 运行结果: [cpp] view plaincopyprint 01.huangcheng@ubuntu:~$ ./a.out 02.结构体struct_data的地址为 0x0xb77db388 03.child1 中 pthread_getspecific(key)返回的指针为:0x0xb77db388 04.利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值: 05.struct_data.i:10 06.struct_data.k: 3.141500 07.------------------------------------------------------ 08.child2 中变量 temp 的地址为 0x0xb6fda38c 09.child2 中 pthread_getspecific(key)返回的指针为:0x0xb6fda38c 10.利用 pthread_getspecific(key)打印 child2 线程中与key关联的整型变量temp 值:20 (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |