您的位置:首页 > 运维架构 > Linux

Linux多线程学习(三)pthread_key_create

2012-09-10 14:19 337 查看
函数 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关联的结构体中成员值:\nstruct_data.i:%d\nstruct_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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: