线程特有数据

线程特有数据的具体实现,不是我们应该关心的。
但是了解它的实现,有助于我们理解它的使用方法。

一种典型的实现是:

  • 进程维护一个全局的数组,存放线程特有数据的键信息。
  • 每个线程包含一个数组,存储每个线程拥有的数据块的指针(通过pthread_setspecific()来存储指针)。

函数

1. pthread_key_create()
#include <pthread.h>
int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
        //Return 0 on success, or a positive error number on error.

调用pthread_key_create()会为线程特有数据创建一个新键,并通过参数key来返回。

因为进程中所有线程都可(理论上,实际上不合适)使用返回的键,所以参数key应指向一个全局变量。

线程终止时,如果与key关联的值不为NULL,就会自动执行解构函数destructor,并且将与key关联的值作为解构函数的参数、

如果无需解构,可以将desturctor设置为NULL。

同一个线程的多个解构函数的调用顺序不确定,因此解构函数应设计为相互独立。

2. pthread_setspecific() ,pthread_getspecific()
#include <pthread.h>

int pthread_setspecific(pthread_key_t key, const void*value);
    Return 0 on success, or a positive error number on error.
void *pthread_getspecific(pthread_key_t key);
    Return pointer, or NULL if no thread-specific data isassociated with key.
3. pthread_once()
#include <pthread.h>

int pthread_once(pthread_once_t *once_control, void (*init)(void));
        Return 0 on success, or a positive error number on error.

多线程程序有时有这样的需求:不管创建来多少线程,有些初始化动作只能发生一次。

这个时候就可以使用pthread_once()

举个例子:
如果直接调用hello_world()函数,那么会打印两个hello world,但是通过pthread_once(),就会值调用一次hello_world(),只会打印一个hello world

#include <stdio.h>
#include <pthread.h>

pthread_once_t int_a = PTHREAD_ONCE_INIT;

void hello_world()
{
	puts ("hello world");
}

void *func_1 (void *arg)
{
	// pthread_once (&int_a, hello_world);
	hello_world();
}

void *func_2 (void *arg)
{
	// pthread_once (&int_a, hello_world);
	hello_world();
}

int main (void)
{
	pthread_t thread_1, thread_2;
	pthread_create (&thread_1, NULL, func_1, NULL);
	pthread_create (&thread_2, NULL, func_2, NULL);
	pthread_join (thread_1, NULL);
	pthread_join (thread_2, NULL);
}
4. pthread_key_delete()

销毁创建的pthread_key_t

#include <pthread.h>

int pthread_key_delete(pthread_key_t key);

示例

创建两个线程,分别给两个线程传入字符串"thread_1"、“thread_2”,这样就可以实现线程安全的函数。

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>

pthread_key_t key_string;
pthread_once_t once_t_create_string = PTHREAD_ONCE_INIT;

void once_create_string()
{
	pthread_key_create (&key_string, NULL);
}

void *func (void *arg)
{
	pthread_once (&once_t_create_string, once_create_string);
	char *string = pthread_getspecific (key_string);
	if (string == NULL) {
		string = strdup (arg);
		pthread_setspecific (key_string, string);
	}
	puts (string);
}

int main (void)
{
	pthread_t thread_1, thread_2;
	pthread_create (&thread_1, NULL, func, "thread_1");
	pthread_create (&thread_2, NULL, func, "thread_2");
	pthread_join (thread_1, NULL);
	pthread_join (thread_2, NULL);
	pthread_key_delete (key_string);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

barbyQAQ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值