pthread_getpecific和pthread_setspecific

本文介绍了如何利用pthread_getspecific和pthread_setspecific在同一个线程的不同函数间共享数据,通过实例展示了具体实现过程。

pthread_getpecific和pthread_setspecific实现同一个线程中不同函数间共享数据的一种很好的方式。

 

#more test.c

/*

 * =====================================================================================

 *       Filename:  thead.c

 *    Description:  getspecific

 *        Created:  05/10/2011 12:09:43 AM

 * =====================================================================================

 */

#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 -lpthread  test.c -o test

# ./test 

2 is runing in thread_func

1 is runing in thread_func

100 is runing in func1

200 is runing in func1

### pthread_setspecific 是否会抛出异常 在 POSIX 线程库中,`pthread_setspecific` 是一个用于设置线程特定数据(Thread-Specific Data, TSD)的函数。与许多 POSIX 函数类似,`pthread_setspecific` 不会抛出 C++ 异常,而是通过返回值来指示是否发生错误。如果调用成功,它将返回 0;如果发生错误,则返回非零的错误码[^1]。 以下是一些可能的错误码及其含义: - `EINVAL`:指定的键无效。 - 其他系统相关的错误码也可能出现。 #### 示例代码及错误处理 以下是一个使用 `pthread_setspecific` 并进行错误处理的示例: ```c #include <pthread.h> #include <stdio.h> #include <stdlib.h> void* thread_func(void* arg) { pthread_key_t key = *(pthread_key_t*)arg; int value = 42; if (pthread_setspecific(key, &value) != 0) { fprintf(stderr, "Error: pthread_setspecific failed\n"); return NULL; } printf("Thread-specific data set successfully\n"); return NULL; } int main() { pthread_t tid; pthread_key_t key; if (pthread_key_create(&key, NULL) != 0) { fprintf(stderr, "Error: pthread_key_create failed\n"); return EXIT_FAILURE; } if (pthread_create(&tid, NULL, thread_func, &key) != 0) { fprintf(stderr, "Error: pthread_create failed\n"); return EXIT_FAILURE; } if (pthread_join(tid, NULL) != 0) { fprintf(stderr, "Error: pthread_join failed\n"); return EXIT_FAILURE; } if (pthread_key_delete(key) != 0) { fprintf(stderr, "Error: pthread_key_delete failed\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } ``` #### 注意事项 当使用 `pthread_setspecific` 时,需要注意以下几点: - 必须先通过 `pthread_key_create` 创建一个有效的键。 - 设置的数据必须在其生命周期内有效,否则可能导致未定义行为。 - 在多线程环境中,确保正确同步资源访问以避免竞争条件或死锁。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值