Linux多线程学习(三)pthread_key_create

函数 pthread_key_create() 用来创建线程私有数据。该函数从 TSD 池中分配一项,将其地址值赋给 key 供以后访问使用。第 2 个参数是一个销毁函数,它是可选的,可以为 NULL,为 NULL 时,则系统调用默认的销毁函数进行相关的数据注销。如果不为空,则在线程退出时(调用 pthread_exit() 函数)时将以 key 锁关联的数据作为参数调用它,以释放分配的缓冲区,或是关闭文件流等。

不论哪个线程调用了 pthread_key_create(),所创建的 key 都是所有线程可以访问的,但各个线程可以根据自己的需要往 key 中填入不同的值,相当于提供了一个同名而不同值的全局变量(这个全局变量相对于拥有这个变量的线程来说)。

注销一个 TSD 使用 pthread_key_delete() 函数。该函数并不检查当前是否有线程正在使用该 TSD,也不会调用清理函数(destructor function),而只是将 TSD 释放以供下一次调用 pthread_key_create() 使用。在 LinuxThread 中,它还会将与之相关的线程数据项设置为 NULL。

#include <stdio.h>

#include <stdlib.h>
#include <pthread.h>
pthread_key_t   key ;
struct   test_struct   {
     int   i ;
     float   k ;
};
void   * child1  ( void   * arg )
{
     struct   test_struct   struct_data ;
     struct_data . i   =   10 ;
     struct_data . k   =   3.1415 ;
     pthread_setspecific  ( key ,   & struct_data );
     printf  ( "结构体struct_data的地址为 0x%p \n " ,   & ( struct_data ));
     printf  ( "child1 中 pthread_getspecific(key)返回的指针为:0x%p \n " ,  ( struct   test_struct   * ) pthread_getspecific ( key ));
     printf  ( "利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值: \n struct_data.i:%d \n struct_data.k: %f \n " ,  (( 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%p \n " ,    & temp );
     pthread_setspecific  ( key ,   & temp );
     printf  ( "child2 中 pthread_getspecific(key)返回的指针为:0x%p \n " ,  ( int   * ) pthread_getspecific ( key ));
     printf  ( "利用 pthread_getspecific(key)打印 child2 线程中与key关联的整型变量temp 值:%d \n " ,   * (( 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 );

}


编译 gcc -o pthread_key_creat -lpthread pthread_key_creat.c

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值