pthread_getspecific和pthread_setspecific使用:同一线程内的各个函数间共享数据

1.pthread_getspecific和pthread_setspecific简介

函数 pthread_setspecific() 将 pointer 的值 (不是锁指的内容) 与key 相关联。
函数 pthread_getspecific() 将与 key 相关联的数据读出来。返回的数据类型都是 void *,因此可以指向任何类型的数据。

在多线程程序中,经常要用全局变量来实现多个函数间的数据共享。由于数据空间是共享的,因此全局变量也为所有线程所共有。
但有时应用程序设计中必要提供线程私有的全局变量,这个变量仅在线程中有效,但却可以跨过多个函数访问。

比如在程序里可能需要每个线程维护一个链表,而会使用相同的函数来操作这个链表,最简单的方法就是使用同名而不
同变量地址的线程相关数据结构。这样的数据结构可以由 Posix 线程库维护,成为线程私有数据 (Thread-specific Data,或称为 TSD)
pthread_getpecific和pthread_setspecific提供了在同一个线程中不同函数间共享数据即线程存储的一种方法。具体用法为:
(1)调用pthread_key_create()来创建一个类型为pthread_key_t类型的变量

该函数有两个参数,第一个参数就是声明的pthread_key_t变量;
第二个参数是一个清理函数,用来在线程释放该线程存储的时候被调用。
该函数指针可以设成NULL,这样系统将调用默认的清理函数。

(2)调用pthread_setspcific()

当线程中需要存储特殊值的时候调用该函数,该函数有两个参数,
第一个为前面声明的pthread_key_t变量,
第二个为void*变量,用来存储任何类型的值。

(3)如果需要取出所存储的值,调用pthread_getspecific()

该函数的参数为前面提到的pthread_key_t变量,该函数返回void*类型的值。

2.pthread_getspecific和pthread_setspecific使用eg1

#include<stdio.h>
#include<pthread.h>
#include<string.h>
pthread_key_t p_key;
 
void func1()
{
        int *tmp = (int*)pthread_getspecific(p_key);//同一线程内的各个函数间共享数据。
        printf("%d is runing in %s\n",*tmp,__func__);
 
}
void *thread_func(void *args)
{
 
        pthread_setspecific(p_key,args);
 
        int *tmp = (int*)pthread_getspecific(p_key);//获得线程的私有空间
        printf("%d is runing in %s\n",*tmp,__func__);
 
        *tmp = (*tmp)*100;//修改私有变量的值
 
        func1();
 
        return (void*)0;
}
int main()
{
        pthread_t pa, pb;
        int a=1;
        int b=2;
        pthread_key_create(&p_key,NULL);
        pthread_create(&pa, NULL,thread_func,&a);
        pthread_create(&pb, NULL,thread_func,&b);
        pthread_join(pa, NULL);
        pthread_join(pb, NULL);
        return 0;
}

#gcc thread.c -o test -lpthread
#./test
1 is runing in thread_func
100 is runing in func1
2 is runing in thread_func
200 is runing in func1

3.pthread_getspecific和pthread_setspecific使用eg2

#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);
}
./pthread_key 
结构体struct_data的地址为 0x0xb7699388
child1 中 pthread_getspecific(key)返回的指针为:0x0xb7699388
利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值:
struct_data.i:10
struct_data.k: 3.141500
------------------------------------------------------
child2 中变量 temp 的地址为 0x0xb6e9838c
child2 中 pthread_getspecific(key)返回的指针为:0x0xb6e9838c
  • 由输出可见,pthread_getspecific() 返回的是与key 相关联数据的指针。
    需要注意的是,在利用这个返回的指针时,它首先是 void 类型的,它虽然指向关联的数据地址处,但并不知道指向的数据类型,所以在具体使用时,要对其进行强制类型转换。
  • 其次,两个线程对自己的私有数据操作是互相不影响的。 也就是说哦,虽然 key 是同名且全局,但访问的内存空间并不是相同的一个。key 就像是一个数据管理员,线程的私有数据只是到他那去注册,让它知道你这个数据的存在。

参考:
https://www.xuebuyuan.com/1608934.html
https://www.jianshu.com/p/d78d93d46fc2

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
pthread_getspecific函数是用于获取线程特定数据(Thread Specific Data, TSD)的函数,它接受一个参数,该参数是一个键(key),它用于标识要获取的线程特定数据。在使用pthread_setspecific函数设置线程特定数据时,需要提供这个键,以便后续使用pthread_getspecific函数获取数据。 具体使用方法如下: 1. 定义一个pthread_key_t类型的变量key,用于标识线程特定数据的键。 2. 在主线程或其他线程中,使用pthread_key_create函数创建这个键,该函数返回0表示成功,-1表示失败。 3. 在需要设置线程特定数据线程中,使用pthread_setspecific函数设置数据,该函数接受两个参数,第一个参数是键,第二个参数是要设置的数据。该函数返回0表示成功,-1表示失败。 4. 在需要获取线程特定数据线程中,使用pthread_getspecific函数获取数据,该函数接受一个参数,即线程特定数据的键,返回值即为该键对应的数据。如果该线程尚未设置过该键对应的数据,返回值为NULL。 5. 在主线程或其他线程中,使用pthread_key_delete函数删除该键,释放相关资源。 下面是一个简单的示例代码: ``` #include <pthread.h> #include <stdio.h> pthread_key_t key; void* thread_func(void* arg) { int* value = (int*)arg; pthread_setspecific(key, value); // 设置线程特定数据 printf("thread %lu: value=%d\n", pthread_self(), *value); return NULL; } int main() { pthread_t tid1, tid2; int value1 = 1, value2 = 2; pthread_key_create(&key, NULL); // 创建线程特定数据的键 pthread_create(&tid1, NULL, thread_func, &value1); pthread_create(&tid2, NULL, thread_func, &value2); pthread_join(tid1, NULL); pthread_join(tid2, NULL); pthread_key_delete(key); // 删除线程特定数据的键 return 0; } ``` 在上面的代码中,我们创建了一个键key,并在两个线程中分别设置了不同的数据(value1和value2),然后在每个线程中使用pthread_getspecific函数获取对应的数据并打印出来。最后,我们在主线程中调用pthread_key_delete函数删除键key。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢打篮球的普通人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值