多线程编程指南

设置线程特定数据

使用 pthread_setspecific(3C) 可以为指定线程特定数据键设置线程特定绑定。

pthread_setspecific 语法

int	pthread_setspecific(pthread_key_t key, const void *value);
#include <pthread.h>



pthread_key_t key;

void *value;

int ret;



/* key previously created */

ret = pthread_setspecific(key, value); 

pthread_setspecific 返回值

pthread_setspecific() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下任一情况,pthread_setspecific() 将失败并返回相应的值。


ENOMEM

描述:

虚拟内存不足。


EINVAL

描述:

key 无效。


注 –

设置新绑定时,pthread_setspecific() 不会释放其存储空间。必须释放现有绑定,否则会出现内存泄漏。